Nginx proxy_pass代理规则
location、proxy_pass都不带 /
location带 /、proxy_pass不带 /
location不带 /、proxy_pass带 /
location、proxy_pass都带 /
location不带 /、proxy_pass后面带目录且带 /
location带 /、proxy_pass后面带目录且带 /
location不带 /、proxy_pass后面带目录且不带 /
location带 /、proxy_pass后面带目录且不带 /
location /test {
proxy_pass http://kk;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/test/aa.txt
location /test/aa {
proxy_pass http://kk;
}
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/test/aa/aa.txt
2、location带 /、proxy_pass不带 /
location /test/ {
proxy_pass http://kk;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/test/aa.txt
location /test/aa/ {
proxy_pass http://kk;
}
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/test/aa/aa.txt
3、location不带 /、proxy_pass带 /
location /test {
proxy_pass http://kk/;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/aa.txt
location /test/aa {
proxy_pass http://kk/;
}
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/aa.txt
4、location、proxy_pass都带 /
location /test/ {
proxy_pass http://kk/;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/aa.txt
location /test/aa/ {
proxy_pass http://kk/;
}
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/aa.txt
5、location不带 /、proxy_pass后面带目录且带 /
location /test {
proxy_pass http://kk/bb/;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa/aa.txt
location /test/aa {
proxy_pass http://kk/bb/;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/bb/.txt
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/cc/aa.txt
代理地址:http://kk/bb/cc/aa.txt
6、location带 /、proxy_pass后面带目录且带 /
location /test/ {
proxy_pass http://kk/bb/;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa/aa.txt
location /test/aa/ {
proxy_pass http://kk/bb/;
}
请求地址: test.com/test/aa.txt
代理地址:无法请求到后端,浏览器显示404
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/cc/aa.txt
代理地址:http://kk/bb/cc/aa.txt
7、location不带 /、proxy_pass后面带目录且不带 /
location /test {
proxy_pass http://kk/bb;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa/aa.txt
location /test/aa {
proxy_pass http://kk/bb;
}
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bb/aa.txt
请求地址: test.com/test/aa/cc/aa.txt
代理地址:http://kk/bb/cc/aa.txt
8、location带 /、proxy_pass后面带目录且不带 /
location /test/ {
proxy_pass http://kk/bb;
}
请求地址: test.com/test/aa.txt
代理地址:http://kk/bbaa.txt
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bbaa/aa.txt
location /test/aa/ {
proxy_pass http://kk/bb;
}
请求地址: test.com/test/aa.txt
代理地址:无法请求到后端,浏览器显示404
请求地址: test.com/test/aa/aa.txt
代理地址:http://kk/bbaa.txt
请求地址: test.com/test/aa/cc/aa.txt
代理地址:http://kk/bbcc/aa.txt
请求地址: test.com/test/aa/cc/dd/aa.txt
代理地址:http://kk/bbcc/dd/aa.txt
总结:
1、当proxy_pass不带目录且不带 / 的情况下,会将location路径中的部分添加到代理地址中
2、当proxy_pass不带目录且带 / 的情况下,会去掉location中的部分,请求地址中多余路径会添加到代理地址中;
3、当proxy_pass带目录的情况,不管目录后面是否带 /,代理地址中都会去掉location部分,且不在location路径中的部分会添加到代理地址中
好了,这就是 Nginx proxy_pass代理的全部匹配规则了,如有问题可与博主一起交流讨论!