diff --git a/b_gdb b/b_gdb new file mode 100644 index 0000000..24e2bf5 --- /dev/null +++ b/b_gdb @@ -0,0 +1,5 @@ +break ngx_http_lua_upstream_get_upstreams +break ngx_http_lua_upstream_get_upstream_main_conf +break ngx_http_lua_upstream_get_servers +break ngx_http_lua_upstream_find_upstream +break ngx_http_lua_upstream_add_server diff --git a/nginx.12085.conf b/nginx.12085.conf new file mode 100644 index 0000000..57f0520 --- /dev/null +++ b/nginx.12085.conf @@ -0,0 +1,123 @@ + +#user nobody; +worker_processes 1; + +#error_log logs/error.log; +#error_log logs/error.log notice; +#error_log logs/error.log info; + +#pid logs/nginx.pid; + + +events { + worker_connections 1024; +} + +# load modules compiled as Dynamic Shared Object (DSO) +# +#dso { +# load ngx_http_fastcgi_module.so; +# load ngx_http_rewrite_module.so; +#} + +http { + include mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + #access_log logs/access.log main; + + sendfile on; + #tcp_nopush on; + + #keepalive_timeout 0; + keepalive_timeout 65; + + #gzip on; + + server { + listen 12085; + server_name localhost; + + #charset koi8-r; + + access_log logs/12085.host.access.log main; + + location / { + root html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} + } + + + # another virtual host using mix of IP-, name-, and port-based configuration + # + #server { + # listen 8000; + # listen somename:8080; + # server_name somename alias another.alias; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + + + # HTTPS server + # + #server { + # listen 443 ssl; + # server_name localhost; + + # ssl_certificate cert.pem; + # ssl_certificate_key cert.key; + + # ssl_session_cache shared:SSL:1m; + # ssl_session_timeout 5m; + + # ssl_ciphers HIGH:!aNULL:!MD5; + # ssl_prefer_server_ciphers on; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + +} diff --git a/nginx.12086.conf b/nginx.12086.conf new file mode 100644 index 0000000..09581f3 --- /dev/null +++ b/nginx.12086.conf @@ -0,0 +1,123 @@ + +#user nobody; +worker_processes 1; + +#error_log logs/error.log; +#error_log logs/error.log notice; +#error_log logs/error.log info; + +#pid logs/nginx.pid; + + +events { + worker_connections 1024; +} + +# load modules compiled as Dynamic Shared Object (DSO) +# +#dso { +# load ngx_http_fastcgi_module.so; +# load ngx_http_rewrite_module.so; +#} + +http { + include mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + #access_log logs/access.log main; + + sendfile on; + #tcp_nopush on; + + #keepalive_timeout 0; + keepalive_timeout 65; + + #gzip on; + + server { + listen 12086; + server_name localhost; + + #charset koi8-r; + + access_log logs/12086.host.access.log main; + + location / { + root html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} + } + + + # another virtual host using mix of IP-, name-, and port-based configuration + # + #server { + # listen 8000; + # listen somename:8080; + # server_name somename alias another.alias; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + + + # HTTPS server + # + #server { + # listen 443 ssl; + # server_name localhost; + + # ssl_certificate cert.pem; + # ssl_certificate_key cert.key; + + # ssl_session_cache shared:SSL:1m; + # ssl_session_timeout 5m; + + # ssl_ciphers HIGH:!aNULL:!MD5; + # ssl_prefer_server_ciphers on; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + +} diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..99cbdc9 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,329 @@ + +#user nobody; +worker_processes 1; + +#error_log logs/error.log; +#error_log logs/error.log notice; +#error_log logs/error.log info; + +#pid logs/nginx.pid; + + +events { + worker_connections 1024; +} + +# load modules compiled as Dynamic Shared Object (DSO) +# +#dso { +# load ngx_http_fastcgi_module.so; +# load ngx_http_rewrite_module.so; +#} + +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.1:12085; + #check interval=3 rise=2 fall=5 timeout=1000 type=http; + #check_http_send "HEAD / HTTP/1.0\r\n\r\n"; + #check_http_expect_alive http_2xx http_3xx; +# server 127.0.0.1:12086; + } + + + include mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + #access_log logs/access.log main; + + sendfile on; + #tcp_nopush on; + + #keepalive_timeout 0; + keepalive_timeout 65; + + #gzip on; + + server { + listen 8080; + server_name localhost; +# 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.187.41.147:81, weight = 1, fail_timeout = 10, max_fails = 1 +# upstream bar: +# + +location = /upstreams { + default_type text/plain; + content_by_lua ' + + local concat = table.concat + local upstream = require "ngx.upstream" + local get_servers = upstream.get_servers + local get_upstreams = upstream.get_upstreams + local add_server = upstream.add_server + + local hello = upstream.say_hello() + ngx.print(hello) + ngx.print("\\n") + + --local err = add_server("bar","127.0.0.5:8080") + --ngx.print(err) + --ngx.print("\\n") + + 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 + '; + } + location /get_servers { + default_type text/plain; + content_by_lua ' + local concat = table.concat + local upstream = require "ngx.upstream" + local get_servers = upstream.get_servers + local get_upstreams = upstream.get_upstreams + --local add_server = upstream.add_server + + local args = ngx.req.get_uri_args() + local upstream_name; + for key, val in pairs(args) do + if type(val) == "table" then + ngx.say(key, ": ", table.concat(val, ", ")) + upstream_name = val + else + ngx.say(key, ": ", val) + upstream_name = val + end + end + + ngx.say("upstream ", upstream_name, ":") + local srvs, err = get_servers(upstream_name) + if not srvs then + ngx.say("failed to get servers in upstream ", upstream_name) + 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 + + + '; + + } + + location /add_server { + + default_type text/plain; + content_by_lua ' + + local concat = table.concat + local upstream = require "ngx.upstream" + local get_servers = upstream.get_servers + local get_upstreams = upstream.get_upstreams + local add_server = upstream.add_server + + local args = ngx.req.get_uri_args() + local upstream_name + + upstream_name = args["upstream"] + local server_ip = args["ip"] + local server_port = args["port"] + local weight = 1 + local max_fails = 10 + local fail_timeout = 10 + + --for key, val in pairs(args) do + --if type(val) == "table" then + --ngx.say(key, ": ", table.concat(val, ", ")) + --upstream_name = val + --else + --ngx.say(key, ": ", val) + --upstream_name = val + --end + --end + + --ngx.say("upstream ", ":", upstream_name ) + --ngx.say("server ", ":", server_ip..":"..server_port) + + local err = add_server("bar",server_ip..":"..server_port,weight,max_fails,fail_timeout) + --ngx.print(err) + --ngx.print("\\n----------------------------\\n") + + local srvs, err = get_servers(upstream_name) + if not srvs then + ngx.say("failed to get servers in upstream ", upstream_name) + 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 + + + '; + } + location /get_primary_peers { + default_type text/plain; + content_by_lua ' + + local upstream = require "ngx.upstream" + local ljson = require "ljson" + us = upstream.get_upstreams() + for _, u in ipairs(us) do + local peers, err = upstream.get_primary_peers(u) + if not peers then + ngx.say("failed to get peers: ", err) + return + end + ngx.say(ljson.encode(peers)) + end + '; + } + + #charset koi8-r; + + access_log logs/1080.host.access.log main; + location /status { + check_status; + + access_log off; + #allow SOME.IP.ADD.RESS; + #deny all; + } + + location / { + proxy_pass http://bar; + #root html; + #index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} + } + + + # another virtual host using mix of IP-, name-, and port-based configuration + # + #server { + # listen 8000; + # listen somename:8080; + # server_name somename alias another.alias; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + + + # HTTPS server + # + #server { + # listen 443 ssl; + # server_name localhost; + + # ssl_certificate cert.pem; + # ssl_certificate_key cert.key; + + # ssl_session_cache shared:SSL:1m; + # ssl_session_timeout 5m; + + # ssl_ciphers HIGH:!aNULL:!MD5; + # ssl_prefer_server_ciphers on; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + +} diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 0d729f5..71f1ad4 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -24,60 +24,61 @@ static int ngx_http_lua_upstream_create_module(lua_State * L); static int ngx_http_lua_upstream_get_upstreams(lua_State * L); static int ngx_http_lua_upstream_get_servers(lua_State * L); static ngx_http_upstream_main_conf_t * - ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L); +ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L); static int ngx_http_lua_upstream_get_primary_peers(lua_State * L); static int ngx_http_lua_upstream_get_backup_peers(lua_State * L); static int ngx_http_lua_get_peer(lua_State *L, - ngx_http_upstream_rr_peer_t *peer, ngx_uint_t id); + ngx_http_upstream_rr_peer_t *peer, ngx_uint_t id); static ngx_http_upstream_srv_conf_t * - ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host); +ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host); static ngx_http_upstream_rr_peer_t * - ngx_http_lua_upstream_lookup_peer(lua_State *L); +ngx_http_lua_upstream_lookup_peer(lua_State *L); static int ngx_http_lua_upstream_set_peer_down(lua_State * L); +static int ngx_http_lua_upstream_say_hello(lua_State* L); +static int +ngx_http_lua_upstream_add_server(lua_State * L); static ngx_http_module_t ngx_http_lua_upstream_ctx = { - NULL, /* preconfiguration */ - ngx_http_lua_upstream_init, /* postconfiguration */ - NULL, /* create main configuration */ - NULL, /* init main configuration */ - NULL, /* create server configuration */ - NULL, /* merge server configuration */ - NULL, /* create location configuration */ - NULL /* merge location configuration */ + NULL, /* preconfiguration */ + ngx_http_lua_upstream_init, /* postconfiguration */ + NULL, /* create main configuration */ + NULL, /* init main configuration */ + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + NULL, /* create location configuration */ + NULL /* merge location configuration */ }; ngx_module_t ngx_http_lua_upstream_module = { NGX_MODULE_V1, - &ngx_http_lua_upstream_ctx, /* module context */ - NULL, /* module directives */ - NGX_HTTP_MODULE, /* module type */ - NULL, /* init master */ - NULL, /* init module */ - NULL, /* init process */ - NULL, /* init thread */ - NULL, /* exit thread */ - NULL, /* exit process */ - NULL, /* exit master */ + &ngx_http_lua_upstream_ctx, /* module context */ + NULL, /* module directives */ + + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ NGX_MODULE_V1_PADDING }; - static ngx_int_t ngx_http_lua_upstream_init(ngx_conf_t *cf) { if (ngx_http_lua_add_package_preload(cf, "ngx.upstream", - ngx_http_lua_upstream_create_module) - != NGX_OK) - { + ngx_http_lua_upstream_create_module) + != NGX_OK) { return NGX_ERROR; } return NGX_OK; } - static int ngx_http_lua_upstream_create_module(lua_State * L) { @@ -98,16 +99,113 @@ ngx_http_lua_upstream_create_module(lua_State * L) lua_pushcfunction(L, ngx_http_lua_upstream_set_peer_down); lua_setfield(L, -2, "set_peer_down"); + lua_pushcfunction(L, ngx_http_lua_upstream_say_hello); + lua_setfield(L, -2, "say_hello"); + + lua_pushcfunction(L, ngx_http_lua_upstream_add_server); + lua_setfield(L, -2, "add_server"); + + // lua_pushcfunction(L, ngx_http_lua_upstream_update_peer_addr); + // lua_setfield(L, -2, "update_peer_addr"); + return 1; } +static int +ngx_http_lua_upstream_say_hello(lua_State * L) +{ + lua_createtable(L, 0, 10); + lua_pushstring(L, "helloword!"); + return 1; +} + +static int +ngx_http_lua_upstream_add_server(lua_State * L) +{ + ngx_str_t host, id; + ngx_http_upstream_server_t *us; + ngx_http_upstream_srv_conf_t *uscf; + ngx_url_t u; + ngx_http_request_t *r; + ngx_int_t weight, max_fails; + time_t fail_timeout; + + if (lua_gettop(L) != 5) { + // four param is :"upstream name", "ip:port" , "weight" , "max_fails", + //"fail_time" + // for lua code , you must pass this four param, is none ,you should + // consider pass default value. + return luaL_error(L, "exactly five argument expected"); + } + + r = ngx_http_lua_get_request(L); + if (r == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "get request error \n"); + return 2; + } + + host.data = (u_char *) luaL_checklstring(L, 1, &host.len); + + ngx_memzero(&u, sizeof (ngx_url_t)); + u.url.data = (u_char *) luaL_checklstring(L, 2, &u.url.len); + u.default_port = 80; + + weight = (ngx_int_t) luaL_checkint(L, 3); + max_fails = (ngx_int_t) luaL_checkint(L, 4); + fail_timeout = (time_t) luaL_checklong(L, 5); +#if (NGX_DEBUG) + ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "%s %s params: %s,%s,%d,%d,%d\n", __FILE__,__FUNCTION__, host.data, u.url.data, weight, max_fails, fail_timeout); +#endif + uscf = ngx_http_lua_upstream_find_upstream(L, &host); + if (uscf == NULL) { + lua_pushnil(L); + lua_pushliteral(L, "upstream not found\n"); + return 2; + } + + + if (uscf->servers == NULL || uscf->servers->nelts == 0) { + //TODO: 对于 默认的空upstream来讲,nginx当前会不允许其启动,可以考虑调整策略,允许此种情况下nginx启动 + // + lua_pushliteral(L, "upstream has no server before!\n"); + lua_newtable(L); + return 2; + } else { + if (ngx_parse_url(uscf->servers->pool, &u) != NGX_OK) { + if (u.err) { + lua_pushnil(L); + lua_pushliteral(L, "url parser error"); + return 2; + } + } + + us = ngx_array_push(uscf->servers); + if (us == NULL) { + lua_pushliteral(L, "us push uscf->servers failed\n"); + return 3; + } + ngx_memzero(us, sizeof (ngx_http_upstream_server_t)); + us->host = u.host; + ngx_str_null(&id); + us->id = id; + us->addrs = u.addrs; + us->naddrs = u.naddrs; + us->weight = weight; + us->max_fails = max_fails; + us->fail_timeout = fail_timeout; + + } + + return 1; +} static int ngx_http_lua_upstream_get_upstreams(lua_State * L) { - ngx_uint_t i; - ngx_http_upstream_srv_conf_t **uscfp, *uscf; - ngx_http_upstream_main_conf_t *umcf; + ngx_uint_t i; + ngx_http_upstream_srv_conf_t **uscfp, *uscf; + ngx_http_upstream_main_conf_t *umcf; if (lua_gettop(L) != 0) { return luaL_error(L, "no argument expected"); @@ -137,14 +235,13 @@ ngx_http_lua_upstream_get_upstreams(lua_State * L) return 1; } - static int ngx_http_lua_upstream_get_servers(lua_State * L) { - ngx_str_t host; - ngx_uint_t i, j, n; - ngx_http_upstream_server_t *server; - ngx_http_upstream_srv_conf_t *us; + ngx_str_t host; + ngx_uint_t i, j, n; + ngx_http_upstream_server_t *server; + ngx_http_upstream_srv_conf_t *us; if (lua_gettop(L) != 1) { return luaL_error(L, "exactly one argument expected"); @@ -186,14 +283,14 @@ ngx_http_lua_upstream_get_servers(lua_State * L) if (server[i].naddrs == 1) { lua_pushlstring(L, (char *) server[i].addrs->name.data, - server[i].addrs->name.len); + server[i].addrs->name.len); } else { lua_createtable(L, server[i].naddrs, 0); for (j = 0; j < server[i].naddrs; j++) { lua_pushlstring(L, (char *) server[i].addrs[j].name.data, - server[i].addrs[j].name.len); + server[i].addrs[j].name.len); lua_rawseti(L, -2, j + 1); } } @@ -230,14 +327,13 @@ ngx_http_lua_upstream_get_servers(lua_State * L) return 1; } - static int ngx_http_lua_upstream_get_primary_peers(lua_State * L) { - ngx_str_t host; - ngx_uint_t i; - ngx_http_upstream_rr_peers_t *peers; - ngx_http_upstream_srv_conf_t *us; + ngx_str_t host; + ngx_uint_t i; + ngx_http_upstream_rr_peers_t *peers; + ngx_http_upstream_srv_conf_t *us; if (lua_gettop(L) != 1) { return luaL_error(L, "exactly one argument expected"); @@ -270,14 +366,13 @@ ngx_http_lua_upstream_get_primary_peers(lua_State * L) return 1; } - static int ngx_http_lua_upstream_get_backup_peers(lua_State * L) { - ngx_str_t host; - ngx_uint_t i; - ngx_http_upstream_rr_peers_t *peers; - ngx_http_upstream_srv_conf_t *us; + ngx_str_t host; + ngx_uint_t i; + ngx_http_upstream_rr_peers_t *peers; + ngx_http_upstream_srv_conf_t *us; if (lua_gettop(L) != 1) { return luaL_error(L, "exactly one argument expected"); @@ -316,11 +411,10 @@ ngx_http_lua_upstream_get_backup_peers(lua_State * L) return 1; } - static int ngx_http_lua_upstream_set_peer_down(lua_State * L) { - ngx_http_upstream_rr_peer_t *peer; + ngx_http_upstream_rr_peer_t *peer; if (lua_gettop(L) != 4) { return luaL_error(L, "exactly 4 arguments expected"); @@ -337,14 +431,13 @@ ngx_http_lua_upstream_set_peer_down(lua_State * L) return 1; } - static ngx_http_upstream_rr_peer_t * ngx_http_lua_upstream_lookup_peer(lua_State *L) { - int id, backup; - ngx_str_t host; - ngx_http_upstream_srv_conf_t *us; - ngx_http_upstream_rr_peers_t *peers; + int id, backup; + ngx_str_t host; + ngx_http_upstream_srv_conf_t *us; + ngx_http_upstream_rr_peers_t *peers; host.data = (u_char *) luaL_checklstring(L, 1, &host.len); @@ -384,12 +477,11 @@ ngx_http_lua_upstream_lookup_peer(lua_State *L) return &peers->peer[id]; } - static int ngx_http_lua_get_peer(lua_State *L, ngx_http_upstream_rr_peer_t *peer, - ngx_uint_t id) + ngx_uint_t id) { - ngx_uint_t n; + ngx_uint_t n; n = 8; @@ -460,32 +552,30 @@ ngx_http_lua_get_peer(lua_State *L, ngx_http_upstream_rr_peer_t *peer, return 0; } - static ngx_http_upstream_main_conf_t * ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L) { - ngx_http_request_t *r; + ngx_http_request_t *r; r = ngx_http_lua_get_request(L); if (r == NULL) { return ngx_http_cycle_get_module_main_conf(ngx_cycle, - ngx_http_upstream_module); + ngx_http_upstream_module); } return ngx_http_get_module_main_conf(r, ngx_http_upstream_module); } - static ngx_http_upstream_srv_conf_t * ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) { - u_char *port; - size_t len; - ngx_int_t n; - ngx_uint_t i; - ngx_http_upstream_srv_conf_t **uscfp, *uscf; - ngx_http_upstream_main_conf_t *umcf; + u_char *port; + size_t len; + ngx_int_t n; + ngx_uint_t i; + ngx_http_upstream_srv_conf_t **uscfp, *uscf; + ngx_http_upstream_main_conf_t *umcf; umcf = ngx_http_lua_upstream_get_upstream_main_conf(L); uscfp = umcf->upstreams.elts; @@ -495,8 +585,7 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) uscf = uscfp[i]; if (uscf->host.len == host->len - && ngx_memcmp(uscf->host.data, host->data, host->len) == 0) - { + && ngx_memcmp(uscf->host.data, host->data, host->len) == 0) { return uscf; } } @@ -518,10 +607,9 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) uscf = uscfp[i]; if (uscf->port - && uscf->port == n - && uscf->host.len == len - && ngx_memcmp(uscf->host.data, host->data, len) == 0) - { + && uscf->port == n + && uscf->host.len == len + && ngx_memcmp(uscf->host.data, host->data, len) == 0) { return uscf; } } @@ -529,3 +617,4 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) return NULL; } + diff --git a/t/sanity.t b/t/sanity.t index dd17159..95748eb 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -564,3 +564,73 @@ upstream 127.0.0.1:1130: --- no_error_log [error] +=== TEST 15: add server with upstream +--- http_config + 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; + } +--- config + + location /add_server { + + default_type text/plain; + content_by_lua ' + + local concat = table.concat + local upstream = require "ngx.upstream" + local get_servers = upstream.get_servers + local get_upstreams = upstream.get_upstreams + local add_server = upstream.add_server + + local args = ngx.req.get_uri_args() + local upstream_name + + upstream_name = args["upstream"] + local server_ip = args["ip"] + local server_port = args["port"] + local weight = 1 + local max_fails = 10 + local fail_timeout = 10 + + + local err = add_server("bar",server_ip..":"..server_port,weight,max_fails,fail_timeout) + + local srvs, err = get_servers(upstream_name) + if not srvs then + ngx.say("failed to get servers in upstream ", upstream_name) + 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 + + + '; + } +--- request + GET /add_server?upstream=bar&ip=127.0.0.9&port=12855 +--- response_body + fail_timeout = 10, addr = 127.0.0.2:80, max_fails = 1, weight = 1 + fail_timeout = 10, addr = 127.0.0.9:12855, max_fails = 10, weight = 1 + +--- no_error_log +[error] \ No newline at end of file