(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
推荐阅读
-
-
荆州安良百货手扶梯绞人事件,如果遇到这种安全隐患要咋自救和救人
-
好听的男孩子名字 儒雅 2022儒雅大气的男孩名字
-
央视网|外交部:澳驻华使馆曾安排两名驻华记者进入使馆以躲避调查 干涉中国内政和司法主权
-
网约车|网约车乘客高速上跳车 后车紧急变道躲过一劫
-
-
『天文』哈勃望远镜,以别样的方式打开我们的视野,引领我们走向浩渺太空
-
-
效仿|街拍:黑色铆钉慵懒毛衣+开叉包臀裙,美女这身精致穿搭值得效仿
-
时尚女装搭配技巧|五一准备去浪的妹子请收藏,大明星秦岚教你出游穿搭
-
[三言财经]华为手机国际市场有衰退,任正非首谈HMS:跟苹果谷歌比差距大
-
噩耗|原创宝财哥走了!《我爱我家》春花扮演者悼念谢园,遗憾错失最后一面
-
-
-
果果妈咪谈育儿|这几种水果不要乱吃,会伤害到胎宝宝和孕妈,怀孕后
-
新咖时尚|都37岁的人,比年轻人还敢穿!,被吴昕惊艳了
-
芯片|显卡一个黑科技逐渐消亡:发烧土豪炫富首选,不符合时代终被淘汰
-
阿超说美食■女儿吃完春卷还想吃,催着我又做了一次,这次的春卷更有滋味!
-
-
车主们注意了!高速公路收费全面调整,司机:幸福来的太突然!