(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
推荐阅读
-
-
[美食]家人最馋此菜,煎一煎,炒一炒,鲜香多汁又下饭,超解馋
-
-
格隆汇APP|格隆汇港股聚焦(10.6)︱绿城中国首九月合同销售额增43%;时代中国控股前九个月销售597亿
-
杭州交通918|急得一宿未睡...,妈妈看了一眼儿子的身份证尾号
-
黑龙江省@截至8日24时 黑龙江累计境外输入确诊病例127例
-
「cnBeta」英国企业和政府宣布打造完全覆盖农村的移动网络
-
财政部:预计59家央企划转国资充实社保约6600亿元
-
-
水果都是怎么生长的?忍了菠萝,忍了雪莲果,最后一种在种花?
-
偶像组合|偶像组合销量被质疑,至少六万张专辑去向不明
-
齐鲁黄河从这里入海|东营北二路沿线及金湖银河片区项目建设快马加鞭,打造宜居生态城市
-
北京日报客户端|柏林主城区疫情超“警戒线”,德国新冠确诊病例超30万例
-
-
-
湖人队|0+0+0!湖人锤爆快船,他却隐身成毒瘤,詹姆斯还要忍他多久?
-
between 和 among 的用法区别 between的用法
-
-
前沿追踪|生死存亡之际华为要靠超越iPhone打翻身仗?库克:这么激进干嘛
-
谈笑者一鸣|搞笑GIF趣图段子:这妹子估计也是和猴子学过两招的