GithubHelp home page GithubHelp logo

lua-upstream-nginx-module's Introduction

Name

ngx_http_lua_upstream - Nginx C module to expose Lua API to ngx_lua for Nginx upstreams

Table of Contents

Status

This module is production ready.

Synopsis

http {
    upstream foo.com {
        server 127.0.0.1 fail_timeout=53 weight=4 max_fails=100;
        server agentzh.org:81;
    }

    upstream bar {
        server 127.0.0.2;
    }

    server {
        listen 8080;

        # sample output for the following /upstream interface:
        # upstream foo.com:
        #     addr = 127.0.0.1:80, weight = 4, fail_timeout = 53, max_fails = 100
        #     addr = 106.184.1.99:81, weight = 1, fail_timeout = 10, max_fails = 1
        # upstream bar:
        #     addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, max_fails = 1

        location = /upstreams {
            default_type text/plain;
            content_by_lua_block {
                local concat = table.concat
                local upstream = require "ngx.upstream"
                local get_servers = upstream.get_servers
                local get_upstreams = upstream.get_upstreams

                local us = get_upstreams()
                for _, u in ipairs(us) do
                    ngx.say("upstream ", u, ":")
                    local srvs, err = get_servers(u)
                    if not srvs then
                        ngx.say("failed to get servers in upstream ", u)
                    else
                        for _, srv in ipairs(srvs) do
                            local first = true
                            for k, v in pairs(srv) do
                                if first then
                                    first = false
                                    ngx.print("    ")
                                else
                                    ngx.print(", ")
                                end
                                if type(v) == "table" then
                                    ngx.print(k, " = {", concat(v, ", "), "}")
                                else
                                    ngx.print(k, " = ", v)
                                end
                            end
                            ngx.print("\n")
                        end
                    end
                end
            }
        }
    }
}

Functions

Back to TOC

get_upstreams

syntax: names = upstream.get_upstreams()

Get a list of the names for all the named upstream groups (i.e., explicit upstream {} blocks).

Note that implicit upstream groups created by proxy_pass and etc are excluded.

Back to TOC

get_servers

syntax: servers = upstream.get_servers(upstream_name)

Get configurations for all the servers in the specified upstream group. Please note that one server may take multiple addresses when its server name can be resolved to multiple addresses.

The return value is an array-like Lua table. Each table entry is a hash-like Lua table that takes the following keys:

  • addr

    socket address(es). can be either a Lua string or an array-like Lua table of Lua strings.

  • backup

  • fail_timeout

  • max_fails

  • name

  • weight

Back to TOC

get_primary_peers

syntax: peers = upstream.get_primary_peers(upstream_name)

Get configurations for all the primary (non-backup) peers in the specified upstream group.

The return value is an array-like Lua table for all the primary peers. Each table entry is a (nested) hash-like Lua table that takes the following keys:

  • current_weight

  • effective_weight

  • fail_timeout

  • fails

  • id

    Identifier (ID) for the peer. This ID can be used to reference a peer in a group in the peer modifying API.

  • max_fails

  • name

    Socket address for the current peer

  • weight

  • accessed

    Timestamp for the last access (in seconds since the Epoch)

  • checked

    Timestamp for the last check (in seconds since the Epoch)

  • down

    Holds true if the peer has been marked as "down", otherwise this key is not present

  • conns

    Number of active connections to the peer (this requires NGINX 1.9.0 or above).

Back to TOC

get_backup_peers

syntax: peers = upstream.get_backup_peers(upstream_name)

Get configurations for all the backup peers in the specified upstream group.

The return value has the same structure as get_primary_peers function.

Back to TOC

set_peer_down

syntax: ok, err = upstream.set_peer_down(upstream_name, is_backup, peer_id, down_value)

Set the "down" (boolean) attribute of the specified peer.

To uniquely specify a peer, you need to specify the upstream name, whether or not it is a backup peer, and the peer id (starting from 0).

Note that this method only changes the peer settings in the current Nginx worker process. You need to synchronize the changes across all the Nginx workers yourself if you want a server-wide change (for example, by means of ngx_lua's ngx.shared.DICT).

Below is an example. Consider we have a "bar" upstream block in nginx.conf:

upstream bar {
    server 127.0.0.2;
    server 127.0.0.3 backup;
    server 127.0.0.4 fail_timeout=23 weight=7 max_fails=200 backup;
}

then

upstream.set_peer_down("bar", false, 0, true)

will turn down the primary peer corresponding to server 127.0.0.2.

Similarly,

upstream.set_peer_down("bar", true, 1, true)

will turn down the backup peer corresponding to server 127.0.0.4 ....

You can turn on a peer again by providing a false value as the 4th argument.

Back to TOC

current_upstream_name

syntax: name = upstream.current_upstream_name()

Returns the name of the proxied upstream for the current request. If there is no upstream for this request (no proxy_pass call), or this function is called in a phase prior to the content phase, then the return value will be nil. If a port is explicitly included in the upstream definition or proxy_pass directive, it will be included in the return value of this function.

Example:

-- upstream my_upstream { ... }
-- proxy_pass http://my_upstream;
upstream.current_upstream_name() --> my_upstream

-- proxy_pass http://example.com:1234;
upstream.current_upstream_name() --> example.com:1234

Note that implicit upstreams created by proxy_pass are included, contrary to the output of upstream.get_upstreams().

Back to TOC

TODO

  • Add API to add or remove servers to existing upstream groups.

Back to TOC

Compatibility

The following versions of Nginx should work with this module:

  • 1.11.x (last tested: 1.11.2)
  • 1.10.x
  • 1.9.x (last tested: 1.9.15)
  • 1.8.x
  • 1.7.x (last tested: 1.7.10)
  • 1.6.x
  • 1.5.x (last tested: 1.5.12)

Back to TOC

Installation

This module is bundled and enabled by default in the OpenResty bundle. And you are recommended to use OpenResty.

  1. Grab the nginx source code from nginx.org, for example, the version 1.11.2 (see nginx compatibility),
  2. then grab the source code of the ngx_lua as well as its dependencies like LuaJIT.
  3. and finally build the source with this module:
wget 'http://nginx.org/download/nginx-1.11.2.tar.gz'
tar -xzvf nginx-1.11.2.tar.gz
cd nginx-1.11.2/

# assuming your luajit is installed to /opt/luajit:
export LUAJIT_LIB=/opt/luajit/lib

# assuming you are using LuaJIT v2.1:
export LUAJIT_INC=/opt/luajit/include/luajit-2.1

# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
    --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
    --add-module=/path/to/lua-nginx-module \
    --add-module=/path/to/lua-upstream-nginx-module

make -j2
make install

Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the --add-dynamic-module=PATH option instead of --add-module=PATH on the ./configure command line above. And then you can explicitly load the module in your nginx.conf via the load_module directive, for example,

load_module /path/to/modules/ngx_http_lua_upstream_module.so;

Back to TOC

Author

Yichun "agentzh" Zhang (章亦春) [email protected], OpenResty Inc.

Back to TOC

Copyright and License

This module is licensed under the BSD license.

Copyright (C) 2014-2017, by Yichun "agentzh" Zhang, OpenResty Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • 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.

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.

Back to TOC

See Also

Back to TOC

lua-upstream-nginx-module's People

Contributors

agentzh avatar chipitsine avatar chronolaw avatar csfrancis avatar jkramarz avatar kipras avatar pushrax avatar thibaultcha avatar xiaocang avatar zhuizhuhaomeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-upstream-nginx-module's Issues

Can I get keepalive and other upstream config

Thanks for your share.

Can I get other config by this module,such as keepalive option along with servers

 server  127.0.0.1:8011 weight=20 max_fails=0 fail_timeout=10s;
 server  127.0.0.1:8012 weight=20 max_fails=0 fail_timeout=10s;
 keepalive 2048;

Wait for your answer sincerely

ngx.upstream module is not accessible from stream-lua-nginx-module

I have nginx-1.10.1 compiled with lua-nginx-module, stream-lua-nginx-module and lua-upstream-nginx-module and I'm trying to implement lua-resty-upstream-healthcheck functionality for tcp stream upstreams.

The following code gives an error when executed in stream { server { ... }} context:

local upstream = require "ngx.upstream"

[error] 9189#9189: *66 stream lua entry thread aborted: runtime error: content_by_lua_block(upstream_tcp.conf:20):2: module 'ngx.upstream' not found:

while the same code works in http { server { ... }} context of my build.

Does stream-lua-nginx-module support lua-upstream-nginx-module?

nginx still use the down peer

I use ngx.upstream.get_primary_peers and find a peer is down。But nginx still use the bad peer。

log:
2018/10/12 16:14:36 [debug] 1998#1998: *508497 [lua] test.lua:21: gen_peers_status_info(): name:123.157.76.2:80stat: up

2018/10/12 16:14:36 [debug] 1998#1998: *508497 [lua] test.lua:21: gen_peers_status_info(): name:123.157.76.5:80stat: DOWN

2018/10/12 16:14:36 [debug] 1998#1998: *508497 [lua] test.lua:21: gen_peers_status_info(): name:60.217.244.95:80stat: up

2018/10/12 16:14:36 [debug] 1998#1998: *508497 http cleanup add: 0000000003439608
2018/10/12 16:14:36 [debug] 1998#1998: *508497 http script var: "/a64/0/ads11euyc1dsa0pb.dsaudio"
2018/10/12 16:14:36 [debug] 1998#1998: *508497 consistent hash [peer name]:123.157.76.5:80 4170160978
2018/10/12 16:14:36 [debug] 1998#1998: *508497 stream socket 18

lua file:
local u = "test.domain.com"
local ok, upstream = pcall(require, "ngx.upstream")
if not ok then
error("ngx_upstream_lua module required")
end
local set_peer_down = upstream.set_peer_down
local get_primary_peers = upstream.get_primary_peers
local get_backup_peers = upstream.get_backup_peers
local get_upstreams = upstream.get_upstreams

local function gen_peers_status_info(peers)
local npeers = #peers
for i = 1, npeers do
local peer = peers[i]
local stat = ""
if peer.down then
stat = " DOWN\n"
else
stat = " up\n"
end
ngx.log(ngx.DEBUG,"name:",peer.name,"stat:",stat)
end
return idx
end

local peers, err = get_primary_peers(u)
if not peers then
return "failed to get primary peers in upstream " .. u .. ": "
.. err
end
gen_peers_status_info(peers)
peers, err = get_backup_peers(u)
if not peers then
return "failed to get backup peers in upstream " .. u .. ": "
.. err
end
gen_peers_status_info(peers)

upstream config :
upstream test.domain.com {
consistent_hash $uri;
#server 123.157.76.2:80 id=1001 weight=1 max_fails=2 fail_timeout=30s;
#server 123.157.76.5:80 id=1002 weight=1 max_fails=2 fail_timeout=30s;
server 123.157.76.2:80;
server 123.157.76.5:80;

    server 60.217.244.95:80 backup;
}

calling tomcat server using lua script

Hi ,
what i am trying to do is.

i want to call tomcat server from nginx using lua script (or any other way), after getting the response from the tomcat server i will decide either i have to forward this request to apache server or serve content from nginx cache.
how this issue can be solved .. and is there any solution.

Note : flow has to be from nginx to tomcat then nginx to apache or browser.
Web ---> nginx <---> tomcat
|
apache

nginx 1.9.12 compatibilty ?

Tried compiling lua-upstream-nginx-module-0.05 and seems to fail on nginx 1.9.12

nginx configure options (alternate ordering):
./configure --with-ld-opt="-ljemalloc -Wl,-z,relro -Wl,-rpath,/usr/local/lib" --with-cc-opt="-m64 -mtune=native -mfpmath=sse -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2" --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_secure_link_module --with-openssl-opt="enable-tlsext" --add-module=../nginx-module-vts --with-libatomic --with-threads --with-stream=dynamic --with-stream_ssl_module --with-http_gzip_static_module --add-dynamic-module=../ngx_brotli --add-dynamic-module=../ngx_pagespeed-release-1.10.33.6-beta --with-http_sub_module --with-http_addition_module --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_realip_module --add-module=../ngx-fancyindex-0.3.6 --add-module=../ngx_cache_purge-2.3 --add-module=../ngx_devel_kit-0.3.0rc1 --add-dynamic-module=../set-misc-nginx-module-0.30 --add-dynamic-module=../echo-nginx-module-0.58 --add-module=../redis2-nginx-module-0.12 --add-module=../ngx_http_redis-0.3.7 --add-dynamic-module=../lua-nginx-module-0.10.2 --add-module=../lua-upstream-nginx-module-0.05 --add-module=../nginx_upstream_check_module-0.3.0 --add-module=../openresty-memc-nginx-module-4f6f78f --add-module=../srcache-nginx-module-0.30 --add-dynamic-module=../headers-more-nginx-module-0.29 --with-pcre=../pcre-8.38 --with-pcre-jit --with-http_ssl_module --with-http_v2_module --with-openssl=../openssl-1.0.2g
checking for OS

  • Linux 2.6.32-042stab112.15 x86_64
    checking for C compiler ... found
  • using GNU C compiler
  • gcc version: 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
configuring additional modules
adding module in ../nginx-module-vts
 + ngx_http_vhost_traffic_status_module was configured
adding module in ../ngx-fancyindex-0.3.6
 + ngx_http_fancyindex_module was configured
adding module in ../ngx_cache_purge-2.3
 + ngx_http_cache_purge_module was configured
adding module in ../ngx_devel_kit-0.3.0rc1
 + ngx_devel_kit was configured
adding module in ../redis2-nginx-module-0.12
 + ngx_http_redis2_module was configured
adding module in ../ngx_http_redis-0.3.7
 + ngx_http_redis_module was configured
adding module in ../lua-upstream-nginx-module-0.05
 + ngx_http_lua_upstream_module was configured
adding module in ../nginx_upstream_check_module-0.3.0
checking for ngx_http_upstream_check_module ... found
 + ngx_http_upstream_check_module was configured
adding module in ../openresty-memc-nginx-module-4f6f78f
 + ngx_http_memc_module was configured
adding module in ../srcache-nginx-module-0.30
 + ngx_http_srcache_filter_module was configured
configuring additional dynamic modules
adding module in ../ngx_brotli
checking for Brotli library ... found
 + ngx_brotli was configured
adding module in ../ngx_pagespeed-release-1.10.33.6-beta
mod_pagespeed_dir=../ngx_pagespeed-release-1.10.33.6-beta/psol/include
build_from_source=false
checking for psol ... found
List of modules (in reverse order of applicability):  ngx_http_write_filter_module ngx_http_header_filter_module ngx_http_chunked_filter_module ngx_http_v2_filter_module ngx_http_range_header_filter_module ngx_http_gzip_filter_module ngx_http_postpone_filter_module ngx_http_ssi_filter_module ngx_http_charset_filter_module ngx_http_sub_filter_module ngx_http_addition_filter_module ngx_http_userid_filter_module ngx_http_headers_filter_module
checking for psol-compiler-compat ... found
 + ngx_pagespeed was configured
adding module in ../set-misc-nginx-module-0.30
found ngx_devel_kit for ngx_set_misc; looks good.
 + ngx_http_set_misc_module was configured
adding module in ../echo-nginx-module-0.58
 + ngx_http_echo_module was configured
adding module in ../lua-nginx-module-0.10.2
checking for LuaJIT library in /usr/local/lib and /usr/local/include/luajit-2.1 (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... found
checking for export symbols by default (-E) ... found
checking for export symbols by default (--export-all-symbols) ... not found
checking for SO_PASSCRED ... found
 + ngx_http_lua_module was configured
adding module in ../headers-more-nginx-module-0.29
 + ngx_http_headers_more_filter_module was configured
checking for zlib library ... found
checking for GD library ... found
checking for GeoIP library ... found
checking for atomic_ops library ... found
creating objs/Makefile
objs/addon/src/ngx_http_lua_upstream_module.o: In function `ngx_http_lua_upstream_get_upstreams':
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:112: undefined reference to `lua_gettop'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:119: undefined reference to `lua_createtable'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:134: undefined reference to `lua_rawseti'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:125: undefined reference to `lua_pushlstring'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:127: undefined reference to `lua_pushfstring'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:128: undefined reference to `lua_concat'
/svr-setup/nginx-1.9.12/../lua-upstream-nginx-module-0.05/src/ngx_http_lua_upstream_module.c:113: undefined reference to `luaL_error'
collect2: error: ld returned 1 exit status
make[1]: *** [objs/nginx] Error 1
make[1]: Leaving directory `/svr-setup/nginx-1.9.12'
make: *** [build] Error 2

cant get upstream.current_upstream_name to work

tried different variants of configuration, no change
config:
$upstream_name used in log_format

server {
.. 
set $upstream_name 'undef';
..
.. 
}
location @siteapi_common {
 ..
  log_by_lua_block {
  ngx.var.upstream_name = "test"
  local upstream = require "ngx.upstream"
  ngx.log(ngx.INFO, "upstream = " .. tostring(upstream.current_upstream_name()))
  local upstream_name = upstream.current_upstream_name()
  ngx.var.upstream_name = upstream_name
  }
..
    proxy_pass $backend_scheme://_bk_api_common;
}

expect to see upstream name (_bk_api_common) in logs but got 'test' string and following in error.log:

2016/12/29 16:13:16 [error] 12547#12547: *20 failed to run log_by_lua*: log_by_lua(api_fallback:36):4: no srv conf for upstream
stack traceback:
        [C]: in function 'current_upstream_name'
        log_by_lua(api_fallback:36):4: in function <log_by_lua(api_fallback:36):1> while logging request, client: ip, server:test, request: "GET /_api/deals/", host: "dev-10--www.testsite"

upstreams are working fine.
What can cause 'no srv conf for upstream' error?

ip_hash has lost while get_servers

使用get_servers获取对应信息,但是获取不到ip_hash,这样会导致我修改后的upstrem持久化后再次reload,ip_hash丢失

how to install new set_peer_weight to openresty

I have no set_peer_weight method from the official download. how to add

我看到程式碼有新的set_peer_weight方法 , 官方的module好像沒有 我該怎新增加去
我是windowns環境

`get_primary_peers` returns `127.255.255.255` instead of name

Hello,

I have upstreams defined like this:

http {
    resolver 1.2.3.4 ipv6=off; 
    
    upstream default {
      server default.hostname.net:8090;
    }
    
    upstream other {
      server other.hostname.net:1234;
    }
    ....
}

other.hostname.net is set up to resolve to 3 different IPs which shows up correctly:

upstream other:
    weight = 1, addr = {10.20.30.40:1234, 11.21.31.41:1234, 12.22.32.42:1234}, name = 127.255.255.255, fail_timeout = 10, max_fails = 1

But I am seeing name come up as 127.255.255.255 instead of other.hostname.net:1234. This is causing healthcheck to fail (I am using the lua-upstream-healthcheck module) because, instead of the 3 separate IPs, it sees the primary peers as 127.255.255.255 and fails to connect there with the following error:

connect() to 127.255.255.255:1234 failed (101: Network is unreachable), context: ngx.timer

Any idea why this may be happening? Do i need to provide a specific resolver for the get_primary_peers call?

Thank you.

max_conns

Hi!
I'm wondering why the max_conns doesn't be exposed in these Lua APIs, one can use it in their own balance implements(like limiting the connections ). Is there any special consideration?

get_primary_peers throw out "table overflow" error

nginx version: 1.11.7
other module: nginx-upstream-jvm-route
the upstream config in nginx.conf:

upstream cluster01 {
server 127.0.0.1:8090 weight=1 max_fails=2 fail_timeout=30s srun_id=test01;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}

When i get this upstream's primary peers , will get the error "table overflow".
Then i tried to gdb it, the error will hit when execute lua_createtable(L, peers->number, 0) statement. Print the peers->number, found the number is large, as the following screen shot
image

But I couldn't found which set the peers->number, could you help to fix this.

set_peer_down functions How to let all worker process effect

hi @agentzh:
Read your document: would modify the peer effect for all the work process that you recommend the use of ngx.shared.DICT. You mean say: put to update the information in the ngx.shared.DICT, and through the timer of each work process from ngx.shared.DICT pull,right? thanks.

get_upstreams's return value contains implicit upstream groups

hello,

by the doc of get_upstreams():

Get a list of the names for all the named upstream groups (i.e., explicit upstream {} blocks).

Note that implicit upstream groups created by proxy_pass and etc are excluded.

But in latest standard openresty-1.19.9.1, this rule breaks.

example confuration below is mostly copyed from the readme, only add a few lines:

events {}
http {
    upstream foo.com {
        server 127.0.0.1 fail_timeout=53 weight=4 max_fails=100;
        server agentzh.org:81;
    }

    upstream bar {
        server 127.0.0.2;
    }

    server {
        listen 8080;

        # sample output for the following /upstream interface:
        # upstream foo.com:
        #     addr = 127.0.0.1:80, weight = 4, fail_timeout = 53, max_fails = 100
        #     addr = 106.184.1.99:81, weight = 1, fail_timeout = 10, max_fails = 1
        # upstream bar:
        #     addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, max_fails = 1

        location /proxy {
            proxy_pass http://127.0.0.1:9999;
        }
        location = /upstreams {
            default_type text/plain;
            content_by_lua_block {
                local concat = table.concat
                local upstream = require "ngx.upstream"
                local get_servers = upstream.get_servers
                local get_upstreams = upstream.get_upstreams

                local us = get_upstreams()
                for _, u in ipairs(us) do
                    ngx.say("upstream ", u, ":")
                    local srvs, err = get_servers(u)
                    if not srvs then
                        ngx.say("failed to get servers in upstream ", u)
                    else
                        for _, srv in ipairs(srvs) do
                            local first = true
                            for k, v in pairs(srv) do
                                if first then
                                    first = false
                                    ngx.print("    ")
                                else
                                    ngx.print(", ")
                                end
                                if type(v) == "table" then
                                    ngx.print(k, " = {", concat(v, ", "), "}")
                                else
                                    ngx.print(k, " = ", v)
                                end
                            end
                            ngx.print("\n")
                        end
                    end
                end
            }
        }
    }
}

when visit curl 127.0.0.1:8080/upstreams ,the result is:

upstream foo.com:
    weight = 4, max_fails = 100, fail_timeout = 53, name = 127.0.0.1, addr = 127.0.0.1:80
    weight = 1, max_fails = 1, fail_timeout = 10, name = agentzh.org:81, addr = 18.138.237.72:81
upstream bar:
    weight = 1, max_fails = 1, fail_timeout = 10, name = 127.0.0.2, addr = 127.0.0.2:80
upstream 127.0.0.1:9999:
    weight = 0, max_fails = 0, addr = 127.0.0.1:9999, fail_timeout = 0

upstream 127.0.0.1:9999 is unwanted implicit upstream, but is included by get_upstreams.

change upstream ip

As tried in a pull a way to change ip of a upstream server, this is the code:

static int
ngx_http_lua_upstream_set_peer_ip(lua_State * L)
{
ngx_str_t host;
ngx_http_upstream_server_t *server;
ngx_http_upstream_srv_conf_t *us;
ngx_http_upstream_rr_peer_t *peer;
ngx_http_upstream_resolved_t *rserver;

if (lua_gettop(L) != 4) {
    return luaL_error(L, "exactly 4 arguments expected");
}

host.data = (u_char *) luaL_checklstring(L, 1, &host.len);

us = ngx_http_lua_upstream_find_upstream(L, &host);
if (us == NULL) {
    lua_pushnil(L);
    lua_pushliteral(L, "upstream not found");
    return 2;
}

server = us->servers->elts;
rserver = us->servers->elts;

server[1].addrs->name.data = (u_char *) lua_tostring(L, 4);
rserver[1].host.data = (u_char *) lua_tostring(L, 4);

peer = ngx_http_lua_upstream_lookup_peer(L);

peer->name.data = (u_char *) lua_tostring(L, 4);
peer->server.data = (u_char *) lua_tostring(L, 4);

lua_pushliteral(L, "name");
lua_pushlstring(L, (char *) peer->name.data, peer->name.len);

return 1;

}

Config:
upstream myLoadBalancerS {
server 192.168.168.2:80 weight=1 fail_timeout=5;
server 192.168.168.26:80 weight=1 fail_timeout=5;
least_conn;
}

Lua code:
location = /luaupstreamip {
default_type text/plain;
content_by_lua '
local upstream = require "ngx.upstream"
ok, err = upstream.set_peer_ip("myLoadBalancerS", false, 1, "192.168.168.17:80")
ngx.print("changed ",ok," ",err,"\n")
';
}

Then following the basic examples to check on the current state;

curl http://127.0.0.1:80/luaupstreams
upstream myLoadBalancerS:
addr = 192.168.168.2:80, weight = 1, fail_timeout = 5, max_fails = 1
addr = 192.168.168.26:80, weight = 1, fail_timeout = 5, max_fails = 1

curl http://127.0.0.1:80/luaupstreamss
[{"current_weight":0,"effective_weight":1,"fail_timeout":5,"fails":0,"id":0,"max
_fails":1,"name":"192.168.168.2:80","server":"192.168.168.2:80","weight":1},{"cu
rrent_weight":0,"effective_weight":1,"fail_timeout":5,"fails":0,"id":1,"max_fail
s":1,"name":"192.168.168.26:80","server":"192.168.168.26:80","weight":1}]

Ok, so far everything looks ok, then I run my new code;

curl http://127.0.0.1:80/luaupstreamip

And check to see what happened;

curl http://127.0.0.1:80/luaupstreams
upstream myLoadBalancerS:
addr = 192.168.168.2:80, weight = 1, fail_timeout = 5, max_fails = 1
addr = 192.168.168.17:80, weight = 1, fail_timeout = 5, max_fails = 1

curl http://127.0.0.1:80/luaupstreamss
[{"current_weight":0,"effective_weight":1,"fail_timeout":5,"fails":0,"id":0,"max
_fails":1,"name":"192.168.168.2:80","server":"192.168.168.2:80","weight":1},{"cu
rrent_weight":0,"effective_weight":1,"fail_timeout":5,"fails":0,"id":1,"max_fail
s":1,"name":"192.168.168.17:80","server":"192.168.168.17:80","weight":1}]

Ok, IP changed, however if I run curl on the location block which does a proxy_pass to myLoadBalancerS, I still see it connecting to 192.168.168.2:80 and 192.168.168.26:80 while the second server (1) should be .17

Obviously I am missing something, it seems the runtime values of the servers do not live in:
ngx_http_upstream_server_t or ngx_http_upstream_srv_conf_t or ngx_http_upstream_rr_peer_t

Any ideas or sample code I can experiment with ?

Does set_peer_down mean the upstream server won't accept any requests?

Hi, Yichun,

We plan to use lua Nginx module for backend server load balance. To prevent out of memory, our upstream server provides configuration for max session number. When the max session number reaches, this server won't participate for load balancing, but the requests for those sessions already created should still be routed to this server. Can I use set_peer_down for the server which has max sessions?

Thanks
Liu Peng

如何保证各 worker 更新 upstream 一致?

这不是 issue, 想了解一下设计。

我理解这个模块是在 nginx 运行时改变 upstream 数据,且一般是 nginx 配置 location, 从一个 http 请求发起修改功能。

那么一次 http 请求会落在一个 worker 上,其他 worker 里面的 upstream 需要同步,同步就需要加锁和等待。请问理解对吗?

我看 nginx plus 是在每个 upstream 里加了 zone 指令, 线上 upstream 的 server 信息会放在 zone里。 每个请求来之后, upstream 负载均衡走 zone 拿实时 server 再往后走, zone 是多个 worker 共享, 那么就不会存在这个问题了。

some confusions to upstream

@agentzh
Hi
I create three TCP clients to connect nginx and make the nginx redirect (use the upstream) to the same server.I think the nginx should build just one TCP connection with the server but it builds three connections,why nginx doesn't reuse the TCP connection.

openresty-1.13.6.1 - get_primary_peers() and get_backup_peers() fail to return a proper peer's tables

Hi,

I have a simple upstream configuration

upstream default {

        zone backend_default 512k;
        
        server 1.1.1.1 max_fails=30 fail_timeout=1;
        server 2.2.2.2 backup;
        server 3.3.3.3 backup;
        keepalive 1000;
}

inside my code I have this

                local backup_servers = upstream_conf.get_backup_peers( "default" );
                ngx.log( ngx.DEBUG, "Backups:\n", utils.serializeTable( backup_servers ));

...which prints this into log file...

2018/05/18 20:12:34 [error] 15726#15726: *5 [lua] tilecache_balancer.lua:113: check_upstreams(): Backups:
{
 1 = {
  current_weight = 0,
  id = 0,
  name = "2.2.2.2:80",
  conns = 0,
  weight = 1,
  fail_timeout = 10,
  effective_weight = 1,
  fails = 0,
  max_fails = 1,
 },
 2 = {
  current_weight = 0,
  id = 1,
  name = "",
  conns = 1.405243031191e+14,
  accessed = 1.4052430311923e+14,
  checked = 12,
  weight = 0,
  fail_timeout = 0,
  effective_weight = 0,
  fails = 15,
  max_fails = 1.4052430312251e+14,
 },
}, client: 127.0.0.1, server: _, request: "GET /health_check HTTP/1.1", host: "127.0.0.1"

...the table for a second backup server looks corrupted ...I have tried to add a second server to primary peers - it also doesn't work properly...

Thanks,
Gene

require 'ngx.upstream' error

lua entry thread aborted: runtime error: /usr/local/nginx/conf/waf.lua:10: module 'ngx.upstream' not found:
no field package.preload['ngx.upstream']

  1. with nginx 1.10.2, and the lastest lua-nginx-modle and the lastest lua-upstream-nginx-module, when require as showed in simple, error happend . no module found. static-module when make

  2. lua-upstream-nginx-module not worked with nginx 1.11 or 1.12

how to work

Implicit upstream not set when proxy_pass is set from a variable

Using openresty/1.11.2.3:

Test config:

daemon off;
http {
    server {
        listen 8080;
        location /upstream {
            content_by_lua_block {
                ngx.say("upstream")
            }
        }
        location /works {
            log_by_lua_block {
                local upstream = require 'ngx.upstream'
                print(upstream.current_upstream_name())
            }
            proxy_pass http://localhost:8080/upstream;
        }
        location /doesnt {
            set $proxy_location http://localhost:8080/upstream;
            log_by_lua_block {
                local upstream = require 'ngx.upstream'
                print(upstream.current_upstream_name())
            }
            proxy_pass $proxy_location;
        }
    }
}
root@29e1d2b24175:~# curl http://localhost:8080/works
127.0.0.1 - - [18/May/2017:02:51:01 +0000] "GET /upstream HTTP/1.0" 200 9 "-" "curl/7.35.0"
2017/05/18 02:51:01 [notice] 5656#0: *1 [lua] log_by_lua(test.conf:21):3: localhost:8080 while logging request, client: 127.0.0.1, server: , request: "GET /works HTTP/1.1", upstream: "http://127.0.0.1:8080/upstream", host: "localhost:8080"
127.0.0.1 - - [18/May/2017:02:51:01 +0000] "GET /works HTTP/1.1" 200 9 "-" "curl/7.35.0"
root@29e1d2b24175:~# curl http://localhost:8080/doesnt
127.0.0.1 - - [18/May/2017:02:51:14 +0000] "GET /upstream HTTP/1.0" 200 9 "-" "curl/7.35.0"
2017/05/18 02:51:14 [error] 5656#0: *4 failed to run log_by_lua*: log_by_lua(test.conf:21):3: no srv conf for upstream
stack traceback:
	[C]: in function 'current_upstream_name'
	log_by_lua(test.conf:21):3: in function <log_by_lua(test.conf:21):1> while logging request, client: 127.0.0.1, server: , request: "GET /doesnt HTTP/1.1", upstream: "http://127.0.0.1:8080/upstream", host: "localhost:8080"
127.0.0.1 - - [18/May/2017:02:51:14 +0000] "GET /doesnt HTTP/1.1" 200 9 "-" "curl/7.35.0"

Request: api to get real server name

Hello @agentzh ,

Thank you for this awesome module and also lua-nginx-module.
I've been trying to work with lua-upstream-nginx-module to create a redirect model inside nginx configuration, in that model, I would like to redirect user to backend address using 301 or 302 response instead of proxy-ing them.

Now I am able to redirect user, all is ok except nginx resolves backend address into ip address which is not necessary in my case.

For example I have configuration below:

upstream {
     server abc.com;
     server def.com
} 

When using upstream-nginx-module, I want to be able to get_upstream_name() will return name=abc.com instead of IP addresses of abc.com.

is it possible? Can you make that api available?

Thanks,

proxy_pass 同一个upstream多端口显示错误

在server中配置
location /upstream1 {
proxy_pass http://127.0.0.1:1190;
}
location /upstream2{
proxy_pass http://127.0.0.2:1110;
}
location /upstream3{
proxy_pass http://127.0.0.1:1130;
}

执行后显示
upstream 127.0.0.1:
fail_timeout = 0, addr = 127.0.0.1:1190, weight = 0, max_fails = 0
upstream 127.0.0.2:
fail_timeout = 0, addr = 127.0.0.2:1110, weight = 0, max_fails = 0
upstream 127.0.0.1:
fail_timeout = 0, addr = 127.0.0.1:1190, weight = 0, max_fails = 0

此时会少显示端口1130,因为在ngx_http_lua_upstream_find_upstream中仅考虑了host名字未考虑端口号

The name returned by get_upstreams is sometimes with a port, sometimes without.

Hello

I use the function get_upstreams to output all the named upstream groups.
I found a strange situation.
In our online environment, most of the displayed upstream names have port 80, and a few do not have port 80.

I would like to know why this is the case and how to remove the port.

nginx version: openresty/1.11.2.2

upstream test1-pool:80:
    1   addr = 10.18.75.163:1189, weight = 1, fail_timeout = 10, name = 10.18.75.163:11889, max_fails = 1
    2   addr = 10.18.96.47:1189, weight = 1, fail_timeout = 10, backup = true, name = 10.18.96.47:11889, max_fails = 1
	
	
upstream test2-pool:
    1   addr = 10.18.38.32:8080, weight = 1, fail_timeout = 10, backup = true, name = 10.18.38.32:8080, max_fails = 1
    2   addr = 10.16.77.94:8080, weight = 1, fail_timeout = 10, name = 10.16.77.94:8080, max_fails = 1

But in my test environment, there is no port with upstream name
nginx version: openresty/1.11.2.2

Feature Request: Add/Delete servers from upstream group

I'm very excited about this module as I've been searching for a way to do this. I'm really hoping for the ability to add/delete servers from an upstream group pro grammatically. At present I'm doing this with perl and it is a big pain in the butt because it requires Regexp::Grammar to parse and render nginx config files (which works).

USE CASE:

I have some LUA code that exposes a network internal set of REST API's to dynamically change certain run time configs and I would love to add this in there!

An example use case for me would be my AWS cloud networks that use Autoscale Groups. With this module/Lua powered endpoints, I can do something like:

curl http://internal-nginx-proxy/upstream/$upstreamname/register

This way the servers/my upstream application are able to signal when they are ready to receive traffic. Alternatively, if the server becomes unhealthy (maybe a full hard drive) my monitoring scripts can remove the server from active rotation like:

curl http://internal-nginx-proxy/upstream/$upstreamname/unregister

This will simplify my cloud automation significantly!

get_primary_peers api does not return the server's status (up or down)?

get_primary_peers api does not return the server's status (up or down), how can i get it ?

the openresty and patch version is as folows:
SZX1000012022:/usr/local/openresty/bin # ./resty -V
resty 0.03
nginx version: openresty/1.9.7.3

patch version:
patch -p0 < /usr/local/plf/nginx_upstream_check_module-master/check_1.9.2+.patch

lua-resty-http module

I am using lua-resty-http module (i.e https://github.com/liseen/lua-resty-http).

In nginx.conf:

Server {

lua_package_path "/root/ngx_openresty-1.5.8.1/build/lua-resty-http/lib/?.lua;;";

location ~ /abc/ {

    set $allow "1";


    content_by_lua '

  local http = require "resty.http"
  local httpc = http.new()
  local res, err = httpc:request_uri("https://example.com/national/DetailPage/test", {
    method = "POST",
    body = "a=1&b=2",
    headers = {
      ["Content-Type"] = "application/x-www-form-urlencoded",
    }
  })

  if not res then
    ngx.say("failed to request: ", err)
    return
  end

  ngx.status = res.status

  ngx.say(res.body)
';
  }

}

content_by_lua code is not executing . can you please help me to get out of this ??

error: ‘ngx_http_upstream_server_t’ has no member named ‘name’

When i compile this module into nginx-1.6.3, it causes an error below:

../lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c: In function ‘ngx_http_lua_upstream_get_servers’:
../lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:179:22: error: ‘ngx_http_upstream_server_t’ has no member named ‘name’
         if (server[i].name.len) {
                      ^
../lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:193:22: error: ‘ngx_http_upstream_server_t’ has no member named ‘name’
         if (server[i].name.len) {
                      ^
../lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:195:50: error: ‘ngx_http_upstream_server_t’ has no member named ‘name’
             lua_pushlstring(L, (char *) server[i].name.data,
                                                  ^
../lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:196:38: error: ‘ngx_http_upstream_server_t’ has no member named ‘name’
                             server[i].name.len);
                                      ^
make[1]: *** [objs/addon/src/ngx_http_lua_upstream_module.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory `/root/nginx_mon/nginx-1.6.3'
make: *** [build] Error 2

I've tried different versions of this module, it is the same error. nginx-1.6.x should still be supported. @agentzh Could you help me out here ?

test case failed

Hi, @agentzh , test case 5 & 6 failed when I run prove t/, report info like bellow:

t/sanity.t .. 18/84 
#   Failed test 'TEST 6: get primary peers: multi-peer servers - response_body_like - response is expected ([{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"202.108.33.60:80","weight":1}])'
#   at /usr/local/share/perl/5.18.2/Test/Nginx/Socket.pm line 1284.
#                   '[{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"202.108.33.60:80","weight":1}]
# '
#     doesn't match '(?^s:^\[\{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"\d{1,3}(?:\.\d{1,3}){3}:80","weight":1\}(?:,\{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":\d+,"max_fails":1,"name":"\d{1,3}(?:\.\d{1,3}){3}:80","weight":1\})+\]$)'

#   Failed test 'TEST 6: get primary peers: multi-peer servers - response_body_like - response is expected ([{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"202.108.33.60:80","weight":1}])'
#   at /usr/local/share/perl/5.18.2/Test/Nginx/Socket.pm line 1284.
#                   '[{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"202.108.33.60:80","weight":1}]
# '
#     doesn't match '(?^s:^\[\{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":0,"max_fails":1,"name":"\d{1,3}(?:\.\d{1,3}){3}:80","weight":1\}(?:,\{"current_weight":0,"effective_weight":1,"fail_timeout":10,"fails":0,"id":\d+,"max_fails":1,"name":"\d{1,3}(?:\.\d{1,3}){3}:80","weight":1\})+\]$)'

#   Failed test 'TEST 5: multi-peer servers - response_body_like - response is expected ([{"addr":"202.108.33.60:80","fail_timeout":10,"max_fails":1,"weight":1}])'
#   at /usr/local/share/perl/5.18.2/Test/Nginx/Socket.pm line 1284.
#                   '[{"addr":"202.108.33.60:80","fail_timeout":10,"max_fails":1,"weight":1}]
# '
#     doesn't match '(?^s:^\[\{"addr":\["\d{1,3}(?:\.\d{1,3}){3}:80"(?:,"\d{1,3}(?:\.\d{1,3}){3}:80")+\],"fail_timeout":10,"max_fails":1,"weight":1\}\]$)'

#   Failed test 'TEST 5: multi-peer servers - response_body_like - response is expected ([{"addr":"202.108.33.60:80","fail_timeout":10,"max_fails":1,"weight":1}])'
#   at /usr/local/share/perl/5.18.2/Test/Nginx/Socket.pm line 1284.
#                   '[{"addr":"202.108.33.60:80","fail_timeout":10,"max_fails":1,"weight":1}]
# '
#     doesn't match '(?^s:^\[\{"addr":\["\d{1,3}(?:\.\d{1,3}){3}:80"(?:,"\d{1,3}(?:\.\d{1,3}){3}:80")+\],"fail_timeout":10,"max_fails":1,"weight":1\}\]$)'
t/sanity.t .. 78/84 # Looks like you failed 4 tests of 84.
t/sanity.t .. Dubious, test returned 4 (wstat 1024, 0x400)
Failed 4/84 subtests 

Test Summary Report
-------------------
t/sanity.t (Wstat: 1024 Tests: 84 Failed: 4)
  Failed tests:  20, 23, 26, 29
  Non-zero exit status: 4

unresolved external symbol2 (2)

error LNK2019: unresolved external symbol _ngx_http_lua_get_request referenced in function _ngx_http_lua_upstream_get_upstream_main_conf

error LNK2019: unresolved external symbol _ngx_http_lua_add_package_preload referenced in function _ngx_http_lua_upstream_init

integrate lua-upstream-module in nginx container

Hi, i've an nginx container used as load balancer, on host port 9200. This is nginx config file:

upstream cdn-audio {
        server 192.168.99.103:9500;
        server 192.168.99.104:9500;
        server 192.168.99.105:9500;
   }

upstream cdn-video {

        server 192.168.99.103:9500;
        server 192.168.99.104:9500;
        server 192.168.99.105:9500;
    }


server {

listen 80;
server_name 172.17.0.1;
access_log /var/log/nginx/acces.log main;

location = /LynyrdSkynyrdFreebirdAudio.mp4 {

#           proxy_pass http://192.168.99.103:9500;

# proxy_pass http://cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4;
add_header X-Upstream  $upstream_addr;
add_header Host $host;
if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin '*';
        add_header Access-Control-Allow-Headers "Authorization,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        add_header Host $host;
        return 200;
    }

       return 302 $scheme://cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4;

        }

location = /LynyrdSkynyrdFreebirdVideo.mp4 {

add_header X-Upstream  $upstream_addr;
#  proxy_pass http://cdn-audio/LynyrdSkynyrdFreebirdVideo.mp4;
add_header Host $host;


if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin '*' ;
        add_header Access-Control-Allow-Headers "Authorization,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        add_header Host $host;
        return 200;
}


#       proxy_pass http://cdn-video$request_uri;

#       proxy_pass http://192.168.99.103:9500;

       return 302 $scheme://cdn-video/LynyrdSkynyrdFreebirdVideo.mp4;
#       add_header X-Upstream  $upstream_addr;

        }

   }

The point is: when i point to localhost:9200/LynyrdSkynyrdFreebirdVideo(or Audio).mp4, the browser cannot resolve "cdn-audio(or video) name".
I've read that your module, and i've seen here, could help me.
Could someone explain me how to integrate this module inside my nginx container?

set_peer_down

Hi, agentzh:
I don't understand how to use the ngx.shared.dict to share the upstream settings in multiple workers(api set_peer_down), can you tell me some tips? Thank you so much!

lua-upstream-nginx-module cann't compile successfully on tengine-2.1.1

Hello,agentzh
when i compile lua-upstream-nginx in tengine-2.1.1,i see same error

/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c
/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c: In function 'ngx_http_lua_upstream_get_servers':
/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:175: error: 'ngx_http_upstream_server_t' has no member named 'name'
/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:189: error: 'ngx_http_upstream_server_t' has no member named 'name'
/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:191: error: 'ngx_http_upstream_server_t' has no member named 'name'
/root/source/tengine/lua-upstream-nginx-module/src/ngx_http_lua_upstream_module.c:192: error: 'ngx_http_upstream_server_t' has no member named 'name'
make[1]: *** [objs/addon/src/ngx_http_lua_upstream_module.o] Error 1
make[1]: Leaving directory `/root/source/tengine/src/tengine-2.1.1'
make: *** [build] Error 2

agentzh,please help me ,thx very very much! :-)

my compile options:
./configure --prefix=/opt/websuite/tengine
--conf-path=/opt/config/tengine/nginx.conf
--error-log-path=/opt/logs/tengine/error.log
--http-log-path=/opt/logs/tengine/access.log
--pid-path=/opt/run/tengine
--user=websuite
--group=websuite
--dso-path=/opt/websuite/tengine/modules
--without-select_module
--without-poll_module
--with-file-aio
--with-http_spdy_module
--with-http_realip_module
--with-http_gzip_static_module
--with-http_auth_request_module
--with-http_addition_module=shared
--with-http_image_filter_module=shared
--with-http_geoip_module=shared
--with-http_concat_module=shared
--with-http_random_index_module=shared
--with-http_secure_link_module=shared
--with-http_sysguard_module=shared
--with-http_charset_filter_module=shared
--with-http_userid_filter_module=shared
--with-http_footer_filter_module=shared
--with-http_trim_filter_module=shared
--with-http_access_module=shared
--with-http_autoindex_module=shared
--with-http_map_module=shared
--with-http_referer_module=shared
--with-http_fastcgi_module=shared
--with-http_uwsgi_module=shared
--with-http_scgi_module=shared
--with-http_empty_gif_module=shared
--with-http_limit_conn_module=shared
--with-http_limit_req_module=shared
--with-http_browser_module=shared
--with-http_user_agent_module=shared
--with-http_upstream_session_sticky_module=shared
--with-http_upstream_ip_hash_module=shared
--with-http_dyups_module
--with-http_reqstat_module=shared
--without-http_ssi_module
--without-http_split_clients_module
--without-http_memcached_module
--without-http_empty_gif_module
--without-http_upstream_least_conn_module
--without-http_upstream_ip_hash_module
--with-http_tfs_module=shared
--with-luajit-inc=/opt/websuite/tengine/include/luajit
--with-luajit-lib=/opt/websuite/tengine/lib
--with-lua-inc=/opt/websuite/tengine/include
--with-lua-lib=/opt/websuite/tengine/lib
--http-client-body-temp-path=/opt/websuite/tengine/cache/client_temp
--http-proxy-temp-path=/opt/websuite/tengine/cache/proxy_temp
--http-fastcgi-temp-path=/opt/websuite/tengine/cache/fastcgi_temp
--http-uwsgi-temp-path=/opt/websuite/tengine/cache/uwsgi_temp
--http-scgi-temp-path=/opt/websuite/tengine/cache/scgi_temp
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
--with-pcre=../pcre-8.36
--with-pcre-opt=-fPIC
--with-pcre-jit
--with-libatomic
--with-jemalloc
--with-libyajl-inc=/usr/local/include/yajl
--with-libyajl-lib=/usr/local/lib
--with-cc-opt='-w -pipe -march=native -mtune=native -m64 -fno-builtin-malloc'
--add-module=/root/source/tengine/echo-nginx-module
--add-module=/root/source/tengine/headers-more-nginx-module
--add-module=/root/source/tengine/memc-nginx-module
--add-module=/root/source/tengine/nginx-module-vts
--add-module=/root/source/tengine/ngx_cache_purge
--add-module=/root/source/tengine/ngx_devel_kit
--add-module=/root/source/tengine/ngx_image_thumb
--add-module=/root/source/tengine/redis2-nginx-module
--add-module=/root/source/tengine/set-misc-nginx-module
--add-module=/root/source/tengine/srcache-nginx-module
--add-module=/root/source/tengine/nginx-hmux-module
--add-module=/root/source/tengine/nginx-access-plus/src/c
--add-module=/root/source/tengine/lua-nginx-module
--add-module=/root/source/tengine/lua-upstream-nginx-module
--add-module=/root/source/tengine/ngx_small_light
--add-module=/root/source/tengine/testcookie-nginx-module

the access.log is error about $upstream_addr not be resloved

the access.log is error about $upstream_addr not be resloved。

the nginx config like this:
upstream api {
keepalive 32;
server x.x.x.x max_fails=5 fail_timeout=60s;
server x.x.x.x max_fails=5 fail_timeout=60s;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" uoid:"$trace_id"'
'---> $upstream_addr <---'
'ups_resp_time: $upstream_response_time '
'request_time: $request_time'
'$request_body';

the access.log error like this:
124.225.204.156 - - [13/Jun/2022:10:03:49 +0800] "GET /api/sync/pkg_prd/list HTTP/1.1" 502 982 "-" "Java/1.8.0_65" "124.225.204.156" uoid:"75c0643c08216071f59369a467e10e3a"---> api <---ups_resp_time: 0.000 request_time: 0.000-

Find peer for request

In short, is it possible to find the peer in an upstream that the current request will be dispatched?
Long version, suppose i have a defined upstream like

upstream zone {
  server e1.example.com;
  server e2.example.com;
  server e3.example.com;
}

If i use sth like

location / {
  proxy_pass http://zone;
}

request will be proxied to e1, e2 or e3. What i want to do is to find out which, without actually creating any secondary request. Something like

location / {
  content_by_lua '
    local upstream = require "ngx.upstream"
    local get_peer = upstream.get_peer
    local peer = get_peer("zone")
    ngx.print(peer)
  ';
}

printing e1.example.com, e2.example.com or e3.example.com depending on the current state and upstream logic. This is particularly useful if upstream is configured with consistent hashing like:

upstream zone {
  hash $uri consistent;
  server e1.example.com;
  server e2.example.com;
  server e3.example.com;
}

So that a function like get_peer will always return the same peer for the same $uri.

I don't know much about nginx internals so the actual question is is this even possible? If so, any pointers would be of great help. I tried to implement it but couldn't get too far.

Thanks

get_primary_peers doesn't return name

Hello, it seems that when "zone" is defined the name filed is returned only for the first peer.

nginx version:
./nginx -v nginx version: openresty/1.9.15.1

upstreams.conf:

upstream http_local {
zone local_zone 64k;
server 127.0.0.1:80 ;
server 127.0.0.2:80 ;
keepalive 16;
}

access.lua:

log(ERR, cjson.encode(upstream.get_primary_peers(upstream_name)))
log(ERR, cjson.encode(upstream.get_servers(upstream_name)))

log.txt:

016/07/19 13:50:00 [error] 80976#0: *92 [lua] access.lua:91: [{"weight":1,"id":0,"conns":0,"fails":0,"current_weight":0,"fail_timeout":10,"effective_weight":1,"name":"127.0.0.1:80","max_fails":1},{"weight":0,"id":1,"conns":0,"fails":0,"current_weight":0,"down":true,"effective_weight":0,"fail_timeout":12,"checked":1.4057793776513e+14,"name":"","max_fails":16}], client: 127.0.0.1, server: _, request: "GET /index.php HTTP/1.1", host: "xxxxx"

2016/07/19 13:50:00 [error] 80976#0: *92 [lua] access.lua:92: [{"addr":"127.0.0.1:80","weight":1,"fail_timeout":10,"name":"127.0.0.1:80","max_fails":1},{"addr":"127.0.0.2:80","weight":1,"fail_timeout":10,"name":"127.0.0.2:80","max_fails":1}], client: 127.0.0.1, server: _, request: "GET /index.php HTTP/1.1", host: "xxxxx"

As you can see in the first log, the second "name " field is empty. If "zone" definition is removed than the result is ok. In contrast the upstream.get_servers method returns always good results.

This issue affects https://github.com/openresty/lua-resty-upstream-healthcheck

module 'ngx.upstream' not found in openresty 1.13.6

when I run the lua code like this:
stream{ preread_by_lua_block{ local upstream = require("ngx.upstream") ... } }
nginx throw the error: module 'ngx.upstream' not found

should I install the module upstream or is it a bug in openresty 1.13.3?

make 的时候出现了一个错误

CentOS 6.5;
NGINX 1.6.2;
LUAJIT 2.0;
... --add-module=/usr/local/ngx_devel_kit-master/ --add-module=/usr/local/lua-nginx-module-master/ --add-module=/usr/local/lua-upstream-nginx-module-master
error info:
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c: 在函数‘ngx_http_lua_upstream_get_servers’中:
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c:179: 错误:‘ngx_http_upstream_server_t’没有名为‘name’的成员
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c:193: 错误:‘ngx_http_upstream_server_t’没有名为‘name’的成员
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c:195: 错误:‘ngx_http_upstream_server_t’没有名为‘name’的成员
/usr/local/lua-upstream-nginx-module-master/src/ngx_http_lua_upstream_module.c:196: 错误:‘ngx_http_upstream_server_t’没有名为‘name’的成员
make[1]: *** [objs/addon/src/ngx_http_lua_upstream_module.o] 错误 1
make[1]: Leaving directory `/nginx-1.6.2'
make: *** [build] 错误 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.