|
| 1 | +Name |
| 2 | +==== |
| 3 | + |
| 4 | +ngx_lua_upstream - Nginx C module to expose Lua API to ngx_lua for Nginx upstreams |
| 5 | + |
| 6 | +Table of Contents |
| 7 | +================= |
| 8 | + |
| 9 | +* [Name](#name) |
| 10 | +* [Status](#status) |
| 11 | +* [Synopsis](#synopsis) |
| 12 | +* [Installation](#installation) |
| 13 | +* [Author](#author) |
| 14 | +* [Copyright and License](#copyright-and-license) |
| 15 | +* [See Also](#see-also) |
| 16 | + |
| 17 | +Status |
| 18 | +====== |
| 19 | + |
| 20 | +This module is still under early development. |
| 21 | + |
| 22 | +Synopsis |
| 23 | +======== |
| 24 | + |
| 25 | +```nginx |
| 26 | +``` |
| 27 | + |
| 28 | + upstream foo.com:1234 { |
| 29 | + server 127.0.0.1 fail_timeout=53 weight=4 max_fails=100; |
| 30 | + server agentzh.org:81; |
| 31 | + } |
| 32 | + |
| 33 | + upstream bar { |
| 34 | + server 127.0.0.2; |
| 35 | + } |
| 36 | + |
| 37 | + server { |
| 38 | + listen 8080; |
| 39 | + |
| 40 | + # sample output for the following /upstream interface: |
| 41 | + # upstream foo.com: |
| 42 | + # addr = 127.0.0.1:80, weight = 4, fail_timeout = 53, max_fails = 100 |
| 43 | + # addr = 106.187.41.147:81, weight = 1, fail_timeout = 10, max_fails = 1 |
| 44 | + # upstream bar: |
| 45 | + # addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, max_fails = 1 |
| 46 | + |
| 47 | + location = /upstreams { |
| 48 | + default_type text/plain; |
| 49 | + content_by_lua ' |
| 50 | + local upstream = require "ngx.upstream" |
| 51 | + local get_servers = upstream.get_servers |
| 52 | + local get_upstreams = upstream.get_upstreams |
| 53 | + |
| 54 | + local us = get_upstreams() |
| 55 | + for _, u in ipairs(us) do |
| 56 | + ngx.say("upstream ", u, ":") |
| 57 | + local srvs, err = get_servers(u) |
| 58 | + if not srvs then |
| 59 | + ngx.say("failed to get servers in upstream ", u) |
| 60 | + else |
| 61 | + for _, srv in ipairs(srvs) do |
| 62 | + local first = true |
| 63 | + for k, v in pairs(srv) do |
| 64 | + if first then |
| 65 | + first = false |
| 66 | + ngx.print(" ") |
| 67 | + else |
| 68 | + ngx.print(", ") |
| 69 | + end |
| 70 | + ngx.print(k, " = ", v) |
| 71 | + end |
| 72 | + ngx.print("\\n") |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + '; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +Compatibility |
| 81 | +============= |
| 82 | + |
| 83 | +The following versions of Nginx should work with this module: |
| 84 | + |
| 85 | +* **1.5.x** (last tested: 1.5.8) |
| 86 | + |
| 87 | +Installation |
| 88 | +============ |
| 89 | + |
| 90 | +1. Grab the nginx source code from [nginx.org](http://nginx.org/), for example, |
| 91 | +the version 1.5.8 (see [nginx compatibility](#compatibility)), |
| 92 | +2. then grab the source code of the [ngx_lua](https://github.com/chaoslawful/lua-nginx-module#installation) as well as its dependencies like [LuaJIT](http://luajit.org/download.html). |
| 93 | +3. and finally build the source with this module: |
| 94 | + |
| 95 | +```bash |
| 96 | +$ wget 'http://nginx.org/download/nginx-1.5.8.tar.gz' |
| 97 | +$ tar -xzvf nginx-1.5.8.tar.gz |
| 98 | +$ cd nginx-1.5.8/ |
| 99 | + |
| 100 | +# assuming your luajit is installed to /opt/luajit: |
| 101 | +export LUAJIT_LIB=/opt/luajit/lib |
| 102 | + |
| 103 | +# assuming you are using LuaJIT 2.0.x: |
| 104 | +export LUAJIT_INC=/opt/luajit/include/luajit-2.0 |
| 105 | + |
| 106 | +# Here we assume you would install you nginx under /opt/nginx/. |
| 107 | +$ ./configure --prefix=/opt/nginx \ |
| 108 | + --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \ |
| 109 | + --add-module=/path/to/lua-nginx-module \ |
| 110 | + --add-module=/path/to/lua-upstream-nginx-module |
| 111 | + |
| 112 | +$ make -j2 |
| 113 | +$ make install |
| 114 | +``` |
| 115 | + |
| 116 | +[Back to TOC](#table-of-contents) |
| 117 | + |
| 118 | +Author |
| 119 | +====== |
| 120 | + |
| 121 | +Yichun "agentzh" Zhang (章亦春) <[email protected]>, CloudFlare Inc. |
| 122 | + |
| 123 | +[Back to TOC](#table-of-contents) |
| 124 | + |
| 125 | +Copyright and License |
| 126 | +===================== |
| 127 | + |
| 128 | +This module is licensed under the BSD license. |
| 129 | + |
| 130 | +Copyright (C) 2014, by Yichun "agentzh" Zhang, CloudFlare Inc. |
| 131 | + |
| 132 | +All rights reserved. |
| 133 | + |
| 134 | +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 135 | + |
| 136 | +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 137 | + |
| 138 | +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 139 | + |
| 140 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 141 | + |
| 142 | +[Back to TOC](#table-of-contents) |
| 143 | + |
| 144 | +See Also |
| 145 | +======== |
| 146 | +* the ngx_lua module: http://github.com/chaoslawful/lua-nginx-module#readme |
| 147 | + |
| 148 | +[Back to TOC](#table-of-contents) |
| 149 | + |
0 commit comments