时间:2020-11-12来源:www.pcxitongcheng.com作者:电脑系统城
1、首先我们回顾一下nginx中location的相关知识
1)location的匹配指令:
2)location 匹配的优先级(与location在配置文件中的顺序无关)
1.= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
2.普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
3.^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
4.最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
2、nginx多个if里面proxy_pass:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
server {listen 127.0.0.1:80; set $test A; set $testB B; location / { if ($test ~* "A") { proxy_pass http://www.so.com; break; } if ($testB ~* "B") { proxy_pass http://www.sogou.com; #break; } }} |
希望满足某个条件的时候,走某个proxy_pass。但是如果多个if都满足,比如上例中的情况:在第一个if中没有break的时候,就会执行下面的;为了第一个匹配上之后就执行proxy_pass,可以加上break。(在nginx中貌似没有if else这样的指令)
3、判断参数进行不同的proxy_pass:
rewrite只能通过url路径进行匹配,不能进行参数匹配,所以如果要实现参数的判断需要用$arg_parameter。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
location / { root html; index index.html index.htm index.php; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_http_version 1.1; proxy_set_header Connection ""; if ( $query_string ~* "usg=0" ) { proxy_pass http://local_workera; } if ( $query_string ~* "usg=1" ) { proxy_pass http://local_workerb; } if ( $arg_uid ~* "(.*[AB]$)" ) { proxy_pass http://local_workerf; } proxy_pass http://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,即:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
location / { root html; index index.html index.htm index.php; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_http_version 1.1; proxy_set_header Connection ""; if ( $arg_uid ~* "(.*[AB]$)" ) { proxy_pass http://local_workerf; break; } if ( $query_string ~* "usg=0" ) { proxy_pass http://local_workera; } if ( $query_string ~* "usg=1" ) { proxy_pass http://local_workerb; } proxy_pass http://local_workera;} |
到此这篇关于nginx location中多个if里面proxy_pass的方法的文章就介绍到这了
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17