天天看点

nginx中localion后路径带或不带/的匹配规则

之前对nginx中localion的匹配规则并没有太多的重视,直到后面上线某个项目时,配置localion中url匹配规则一直报错,才专门又总结了一下localion后面‘/’的截取规则。

‘/’的匹配大概分为两类,proxy_pass带地址目录和不带地址目录,简单来说就是看proxy_pass的ip:port后还有没有东西。总结如下:

1.proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部。

2.proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录。

下面对两种情况介绍:

情况一,proxy_pass代理地址端口后无任何。

此时注意proxy_pass的转发,只有IP和端口,127.0.0.1:8080,其余无任何。

location /test1/ {
    proxy_pass http://127.0.0.1:8080;
}
#或者
location /test1 {
    proxy_pass http://127.0.0.1:8080;
}
           

此时,若请求为www.test1.com/test1/test11,匹配到/test1/或/test后,按照“代理地址+访问URL目录”的规则,将会转发到http://127.0.0.1:8080/test1/test11。

这种情况比较容易理解。

情况二,proxy_pass代理地址端口后包含目录

此时注意proxy_pass的转发,除了IP和端口,还有‘/’或者‘/XXXX’等情况。

#1.proxy_pass变化
location /test1/ {
    proxy_pass http://127.0.0.1:8080/;
}
#www.test1.com/test1/test11->http://127.0.0.1:8080/test11
 
#或者
location /test1/ {
    proxy_pass http://127.0.0.1:8080/test2/;
}
#www.test1.com/test1/test11->http://127.0.0.1:8080/test2/test11
 
#2.location匹配变化
location /test1/ {
    proxy_pass http://127.0.0.1:8080/test2;
}
#www.test1.com/test1/test11->http://127.0.0.1:8080/test2test11(截取了/test1/,少个‘/’)
 
#或者
location /test1 {
    proxy_pass http://127.0.0.1:8080/test2;
}
#www.test1.com/test1/test11->http://127.0.0.1:8080/test2/test11
           

这种情况下,nginx转发会对localion路径下的匹配进行截取

,按照“代理地址+访问URL目录部分去除location匹配目录”的规则,重新进行拼接后转发。

继续阅读