(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
推荐阅读
-
-
-
霸气的小猪佩琦|生死战23分惨案!湖人连赢4场太狠,休城面临重建
-
「久期财经」展望“稳定”,惠誉:确认蓝色光标(300058.SZ)“B+”长期本外币发行人评级
-
|84岁老爷爷参加高考,考前自估630,得知成绩后欲哭无泪
-
-
-
好声音■《好声音》导师阵容确定,没了沈腾贾玲搭档,他能适应吗?
-
利利讲快乐|东方卫视新综艺亮相,放弃《欢乐喜剧人》了吗,90%喜剧大咖聚齐
-
特斯拉|金钱在燃烧 马斯克:特斯拉柏林、德州超级工厂正亏损数十亿美元
-
开屏|杨丽萍到公园偶遇孔雀,孔雀纷纷开屏迎接,万物皆有灵!
-
-
尤文图斯|虽然来得晚些,尤文图斯仍如愿捧杯,C罗续写着他的辉煌
-
睡前常做4种行为,容易诱发失眠,做的越多,失眠的情况越严重
-
演唱会|经典音乐人:开演唱会都不用自己开口的人!全是观众在唱——伍佰
-
-
-
-
-