nginx location中多个if里面proxy_pass的方法
1、首先我们回顾一下nginx中location的相关知识
1)location的匹配指令:
- ~ #波浪线表示执行一个正则匹配,区分大小写
- ~* #表示执行一个正则匹配,不区分大小写
- ^~ #^~表示普通字符匹配,不是正则匹配。如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
- = #进行普通字符精确匹配
- @ #"@"定义一个命名的location,使用在内部定向时,例如error_page,try_files
2)location匹配的优先级(与location在配置文件中的顺序无关)
1.=精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
2.普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
3.^~则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
4.最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
2、nginx多个if里面proxy_pass:
server{ listen127.0.0.1:80; set$testA; set$testBB; location/{ if($test~*"A"){ proxy_passhttp://www.so.com; break; } if($testB~*"B"){ proxy_passhttp://www.sogou.com; #break; } } }
希望满足某个条件的时候,走某个proxy_pass。但是如果多个if都满足,比如上例中的情况:在第一个if中没有break的时候,就会执行下面的;为了第一个匹配上之后就执行proxy_pass,可以加上break。(在nginx中貌似没有ifelse这样的指令)
3、判断参数进行不同的proxy_pass:
rewrite只能通过url路径进行匹配,不能进行参数匹配,所以如果要实现参数的判断需要用$arg_parameter。
location/{ roothtml; indexindex.htmlindex.htmindex.php; proxy_redirectoff; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerHost$http_host; proxy_http_version1.1; proxy_set_headerConnection""; if($query_string~*"usg=0"){ proxy_passhttp://local_workera; } if($query_string~*"usg=1"){ proxy_passhttp://local_workerb; } if($arg_uid~*"(.*[AB]$)"){ proxy_passhttp://local_workerf; } proxy_passhttp://local_workera; }
1)请求路径中的usg=0和usg=1这两个参数是确定的,所以使用了$query_string进行正则匹配即可;($query_string的值是请求中所有参数)
2)接下来,我们想对uid的值如果是以A、B结尾的请求,转向local_workerf处理,这时候就无法用$query_string进行正则匹配了;(因为对于/?uid=1A&t=1&usg=1和/?uid=123&t=A&usg=0不太好匹配)这时,只能用$arg_uid进行正则匹配了。
3)由于usg=0和usg=2这两个参数是互斥的,所以根据上面location中if指令的逻辑,不用break也可以正确处理,且放到最上面。对于uid的匹配,由于会和usg进行冲突,所以只能放到最下面或者加break,即:
location/{ roothtml; indexindex.htmlindex.htmindex.php; proxy_redirectoff; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerHost$http_host; proxy_http_version1.1; proxy_set_headerConnection""; if($arg_uid~*"(.*[AB]$)"){ proxy_passhttp://local_workerf; break; } if($query_string~*"usg=0"){ proxy_passhttp://local_workera; } if($query_string~*"usg=1"){ proxy_passhttp://local_workerb; } proxy_passhttp://local_workera; }
到此这篇关于nginxlocation中多个if里面proxy_pass的方法的文章就介绍到这了,更多相关nginxlocationproxy_pass内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!