西红柿小生 利用nginx实现生产和灰度环境流量切换


(1)、使用if指令实现upstreamserver_01{server192.168.1.100:8080max_fails=1fail_timeout=60;}upstreamserver_02{server192.168.1.101:8080max_fails=1fail_timeout=60;}server{listen80;server_namewww.server.com;#matchcookieset$server"server_01";if($http_cookie~*"version=V1"){set$serverserver_01;}if($http_cookie~*"version=V2"){set$serverserver_02;}location/{proxy_passhttp://$server;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}(2)、使用map指令实现upstreamserver_01{server192.168.1.100:8080max_fails=1fail_timeout=60;}upstreamserver_02{server192.168.1.101:8080max_fails=1fail_timeout=60;}map$COOKIE_version$server{~*V1$server_01;~*V2$server_02;defaultserver_01;}server{listen80;server_namewww.server.com;location/{proxy_passhttp://$server;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}2、根据权重实现灰度配置upstreamserver{server192.168.1.100:8080max_fails=1fail_timeout=60weight=5;server192.168.1.101:8080max_fails=1fail_timeout=60weight=1;}server{listen80;server_namewww.server.com;location/{proxy_passhttp://server;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}3、根据来路IP实现灰度配置(1)、资源在不同服务器上upstreamserver_01{server192.168.1.100:8080max_fails=1fail_timeout=60;}upstreamserver_02{server192.168.1.101:8080max_fails=1fail_timeout=60;}server{listen80;server_namewww.server.com;set$server"server_01";if($remote_addr~"211.118.119.11"){set$serverserver_02;}location/{proxy_passhttp://$server;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}(2)、资源在同一服务器上server{listen80;server_namewww.server.com;set$rootdir"/var/www/html";if($remote_addr~"211.118.119.11"){set$rootdir"/var/www/test";}location/{root$rootdir;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;indexindex.htmlindex.htm;}}4、Nginx+Lua实现灰度配置#如果来源IP地址是ipList.conf中的地址 , 那么就定位到test_env , 如果来源IP地址不在ipList.conf中(即有效用户IP) , 那么就定位到product_env 。 localip_config=ngx.shared.config;ClienIP=ngx.req.get_headers()["X-Real-IP"]ifClientIP==nilthenClientIP=ngx.req.get_headers()["x_forworded_for"]endifClientIP==nilthenClientIP=ngx.var.remote_addrendforlineinio.lines("/usr/local/nginx/conf/lua/ipList.conf")doifnotip_config:get(line)thenip_config:set(line,"0")endendifip_config:get(ClientIP)=="0"thenngx.exec("@test_env")elsengx.exec("@product_env")endipList.conf内容格式XXX.XXX.XXX.XXXYYY.YYY.YYY.YYYlua_code_cacheon;lua_shared_dictconfig1m;upstreamMyServer{server192.168.1.199:8099max_fails=3fail_timeout=30s;server192.168.1.200:8099max_fails=3fail_timeout=30s;ip_hash;}server{listen80;server_namejokerzhang.cn;access_logoff;#access_loglogs/jokerzhang.log;location/{access_by_lua_file/usr/local/nginx/conf/lua/ip_gray.lua;}location@test_env{proxy_pass;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}location@product_env{proxy_passhttp://MyServer;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;}}作者:Fantasy


推荐阅读