From 39ce03d1eb92a1930e314f51d53fdae7c0898b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=A5=E7=94=9F?= Date: Tue, 10 Mar 2015 22:12:02 +0800 Subject: [PATCH 1/5] add add_server function and nginx.conf ,but not full test,and the respawn worker has no add data --- b_gdb | 5 + nginx.conf | 300 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100644 b_gdb create mode 100644 nginx.conf 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.conf b/nginx.conf new file mode 100644 index 0000000..0bf9e52 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,300 @@ + +#user nobody; +worker_processes 8; + +#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.2; + } + + + 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 + + + '; + } + + #charset koi8-r; + + #access_log logs/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; + # } + #} + +} From 33b2703e25c4343124e43073b24012e564077457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=A5=E7=94=9F?= Date: Tue, 10 Mar 2015 22:13:55 +0800 Subject: [PATCH 2/5] add add_server function and nginx.conf ,but not full test,and the respawn worker has no add data --- src/ngx_http_lua_upstream_module.c | 345 +++++++++++++++++++++++------ 1 file changed, 272 insertions(+), 73 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 0d729f5..5629209 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,6 +99,103 @@ 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); + ngx_log_error(NGX_LOG_EMERG,r->connection->log,0,"%s,%s,%d,%d,%d\n",host.data,u.url.data,weight,max_fails,fail_timeout); + + 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 (ngx_parse_url(r->pool, &u) != NGX_OK) { + if (u.err) { + lua_pushnil(L); + lua_pushliteral(L, "url parser error"); + 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 { + us = ngx_array_push(uscf->servers); + if (us == NULL) { + lua_pushliteral(L, "us push uscf->servers failed\n"); + return 3; + } + + us->host = u.host; + ngx_str_null(&id); + + us->addrs = u.addrs; + us->naddrs = u.naddrs; + us->host = u.host; + us->weight = weight; + us->max_fails = max_fails; + us->fail_timeout = fail_timeout; + us->id = id; + } + return 1; } @@ -105,9 +203,9 @@ ngx_http_lua_upstream_create_module(lua_State * L) 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"); @@ -171,6 +268,14 @@ ngx_http_lua_upstream_get_servers(lua_State * L) for (i = 0; i < us->servers->nelts; i++) { n = 4; + // for server marked "backup" or "down" ,also should be reported + // if (server[i].backup) { + // n++; + // } + // + // if (server[i].down) { + // n++; + // } if (server[i].backup) { n++; @@ -186,14 +291,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 +335,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 +374,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 +419,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 +439,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 +485,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 +560,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 +593,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 +615,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 +625,106 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) return NULL; } + + +//static int +//ngx_http_lua_upstream_update_peer_addr(lua_State *L) +//{ +// ngx_str_t host, p; +// ngx_url_t url; +// ngx_uint_t id; +// ngx_http_request_t *r; +// ngx_http_upstream_rr_peer_t *peer; +// ngx_http_upstream_server_t *server; +// ngx_http_upstream_srv_conf_t *uscf; +// +// if (lua_gettop(L) != 4) { +// return luaL_error(L, "exactly 4 arguments expected"); +// } +// +// host.data = (u_char *) luaL_checklstring(L, 1, &host.len); +// +// uscf = ngx_http_lua_upstream_find_upstream(L, &host); +// if (uscf == NULL) { +// lua_pushnil(L); +// lua_pushliteral(L, "upstream not found"); +// return 2; +// } +// +// r = ngx_http_lua_get_request(L); +// if (r == NULL) { +// return luaL_error(L, "no request found"); +// } +// +// ngx_memzero(&url, sizeof(ngx_url_t)); +// +// url.url.data = (u_char *) lua_tolstring(L, 4, (size_t *) &url.url.len); +// url.default_port = 80; +// url.no_resolve = 1; +// +// if (ngx_parse_url(r->pool, &url) != NGX_OK) { +// lua_pushnil(L); +// +// if (url.err) { +// lua_pushfstring(L, "failed to parse host name \"%s\": %s", +// url.url.data, url.err); +// +// } else { +// lua_pushfstring(L, "failed to parse host name \"%s\"", url.url.data); +// } +// +// return 2; +// } +// +//#if (NGX_DEBUG) +// u_char text[NGX_SOCKADDR_STRLEN]; +// ngx_str_t addr; +// addr.data = text; +// if (url.addrs && url.addrs[0].sockaddr) { +// addr.len = ngx_sock_ntop(url.addrs[0].sockaddr, url.addrs[0].socklen, +// text, NGX_SOCKADDR_STRLEN, 0); +// ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, +// "set_peer_addr in lua_upstream_module set to %V", &addr); +// } +//#endif +// +// if (url.addrs && url.addrs[0].sockaddr) { +// peer = ngx_http_lua_upstream_lookup_peer(L); +// if (peer == NULL) { +// return 2; +// } +// +// /* basically allocated pool size is the maximum length of ip address */ +// p.data = peer->name.data; +// p.len = url.addrs[0].name.len; +// +// if (uscf->servers && !uscf->port) { +// server = uscf->servers->elts; +// +// id = (ngx_uint_t) lua_tonumber(L, 3); +// +// ngx_memcpy(p.data, url.addrs[0].name.data, p.len); +// +// server[id].addrs->name = p; +// peer->name = p; +// peer->server = p; +// +// ngx_memcpy(peer->sockaddr, url.addrs[0].sockaddr, peer->socklen); +// +// ngx_memcpy(server[id].addrs->sockaddr, url.addrs[0].sockaddr, server[id].addrs->socklen); +// } else { +// ngx_memcpy(p.data, url.addrs[0].name.data, p.len); +// +// peer->name = p; +// +// if (peer->server.data) { +// peer->server = p; +// } +// +// ngx_memcpy(peer->sockaddr, url.addrs[0].sockaddr, peer->socklen); +// } +// } +// +// lua_pushboolean(L, 1); +// return 1; +//} From 3ce024c15a1aea88aa44a9036107b2d63ade7724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=A5=E7=94=9F?= Date: Mon, 16 Mar 2015 16:25:37 +0800 Subject: [PATCH 3/5] =?UTF-8?q?debug=20uscf->servers->pool=20,=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=20add=5Fserver=20=E5=87=BD=E6=95=B0=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?server=E9=94=99=E8=AF=AF=E7=9A=84=E5=86=85=E5=AD=98=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=EF=BC=8C=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ngx_http_lua_upstream_module.c | 142 ++++------------------------- 1 file changed, 16 insertions(+), 126 deletions(-) diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c index 5629209..71f1ad4 100644 --- a/src/ngx_http_lua_upstream_module.c +++ b/src/ngx_http_lua_upstream_module.c @@ -137,14 +137,14 @@ ngx_http_lua_upstream_add_server(lua_State * L) // 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)); @@ -154,8 +154,9 @@ ngx_http_lua_upstream_add_server(lua_State * L) 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); - ngx_log_error(NGX_LOG_EMERG,r->connection->log,0,"%s,%s,%d,%d,%d\n",host.data,u.url.data,weight,max_fails,fail_timeout); - +#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); @@ -163,13 +164,6 @@ ngx_http_lua_upstream_add_server(lua_State * L) return 2; } - if (ngx_parse_url(r->pool, &u) != NGX_OK) { - if (u.err) { - lua_pushnil(L); - lua_pushliteral(L, "url parser error"); - return 2; - } - } if (uscf->servers == NULL || uscf->servers->nelts == 0) { //TODO: 对于 默认的空upstream来讲,nginx当前会不允许其启动,可以考虑调整策略,允许此种情况下nginx启动 @@ -178,28 +172,34 @@ ngx_http_lua_upstream_add_server(lua_State * L) 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->host = u.host; us->weight = weight; us->max_fails = max_fails; us->fail_timeout = fail_timeout; - us->id = id; + } return 1; } - static int ngx_http_lua_upstream_get_upstreams(lua_State * L) { @@ -268,14 +268,6 @@ ngx_http_lua_upstream_get_servers(lua_State * L) for (i = 0; i < us->servers->nelts; i++) { n = 4; - // for server marked "backup" or "down" ,also should be reported - // if (server[i].backup) { - // n++; - // } - // - // if (server[i].down) { - // n++; - // } if (server[i].backup) { n++; @@ -626,105 +618,3 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host) return NULL; } - -//static int -//ngx_http_lua_upstream_update_peer_addr(lua_State *L) -//{ -// ngx_str_t host, p; -// ngx_url_t url; -// ngx_uint_t id; -// ngx_http_request_t *r; -// ngx_http_upstream_rr_peer_t *peer; -// ngx_http_upstream_server_t *server; -// ngx_http_upstream_srv_conf_t *uscf; -// -// if (lua_gettop(L) != 4) { -// return luaL_error(L, "exactly 4 arguments expected"); -// } -// -// host.data = (u_char *) luaL_checklstring(L, 1, &host.len); -// -// uscf = ngx_http_lua_upstream_find_upstream(L, &host); -// if (uscf == NULL) { -// lua_pushnil(L); -// lua_pushliteral(L, "upstream not found"); -// return 2; -// } -// -// r = ngx_http_lua_get_request(L); -// if (r == NULL) { -// return luaL_error(L, "no request found"); -// } -// -// ngx_memzero(&url, sizeof(ngx_url_t)); -// -// url.url.data = (u_char *) lua_tolstring(L, 4, (size_t *) &url.url.len); -// url.default_port = 80; -// url.no_resolve = 1; -// -// if (ngx_parse_url(r->pool, &url) != NGX_OK) { -// lua_pushnil(L); -// -// if (url.err) { -// lua_pushfstring(L, "failed to parse host name \"%s\": %s", -// url.url.data, url.err); -// -// } else { -// lua_pushfstring(L, "failed to parse host name \"%s\"", url.url.data); -// } -// -// return 2; -// } -// -//#if (NGX_DEBUG) -// u_char text[NGX_SOCKADDR_STRLEN]; -// ngx_str_t addr; -// addr.data = text; -// if (url.addrs && url.addrs[0].sockaddr) { -// addr.len = ngx_sock_ntop(url.addrs[0].sockaddr, url.addrs[0].socklen, -// text, NGX_SOCKADDR_STRLEN, 0); -// ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, -// "set_peer_addr in lua_upstream_module set to %V", &addr); -// } -//#endif -// -// if (url.addrs && url.addrs[0].sockaddr) { -// peer = ngx_http_lua_upstream_lookup_peer(L); -// if (peer == NULL) { -// return 2; -// } -// -// /* basically allocated pool size is the maximum length of ip address */ -// p.data = peer->name.data; -// p.len = url.addrs[0].name.len; -// -// if (uscf->servers && !uscf->port) { -// server = uscf->servers->elts; -// -// id = (ngx_uint_t) lua_tonumber(L, 3); -// -// ngx_memcpy(p.data, url.addrs[0].name.data, p.len); -// -// server[id].addrs->name = p; -// peer->name = p; -// peer->server = p; -// -// ngx_memcpy(peer->sockaddr, url.addrs[0].sockaddr, peer->socklen); -// -// ngx_memcpy(server[id].addrs->sockaddr, url.addrs[0].sockaddr, server[id].addrs->socklen); -// } else { -// ngx_memcpy(p.data, url.addrs[0].name.data, p.len); -// -// peer->name = p; -// -// if (peer->server.data) { -// peer->server = p; -// } -// -// ngx_memcpy(peer->sockaddr, url.addrs[0].sockaddr, peer->socklen); -// } -// } -// -// lua_pushboolean(L, 1); -// return 1; -//} From b3e00fe28e1cbdf9d03c9c476ed079b1335a3832 Mon Sep 17 00:00:00 2001 From: "chunshengster@gmail.com" <> Date: Mon, 16 Mar 2015 20:18:45 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- t/sanity.t | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) 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 From 770d1dfb366ed9b2a8196eed5bc21fca03e2c713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=A5=E7=94=9F?= Date: Tue, 17 Mar 2015 14:37:12 +0800 Subject: [PATCH 5/5] add test configuration file --- nginx.12085.conf | 123 +++++++++++++++++++++++++++++++++++++++++++++++ nginx.12086.conf | 123 +++++++++++++++++++++++++++++++++++++++++++++++ nginx.conf | 73 +++++++++++++++++++--------- 3 files changed, 297 insertions(+), 22 deletions(-) create mode 100644 nginx.12085.conf create mode 100644 nginx.12086.conf 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 index 0bf9e52..99cbdc9 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,6 +1,6 @@ #user nobody; -worker_processes 8; +worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; @@ -27,16 +27,20 @@ http { } upstream bar { - server 127.0.0.2; + 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"'; + 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; @@ -175,22 +179,22 @@ location = /upstreams { 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) + --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") + --ngx.print(err) + --ngx.print("\\n----------------------------\\n") local srvs, err = get_servers(upstream_name) if not srvs then @@ -218,14 +222,39 @@ location = /upstreams { '; } + 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/host.access.log main; + access_log logs/1080.host.access.log main; + location /status { + check_status; + + access_log off; + #allow SOME.IP.ADD.RESS; + #deny all; + } location / { - root html; - index index.html index.htm; + proxy_pass http://bar; + #root html; + #index index.html index.htm; } #error_page 404 /404.html;