Nginx proxy_pass代理规则

Nginx proxy_pass代理规则

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后面带目录且不带 /


1、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代理的全部匹配规则了,如有问题可与博主一起交流讨论!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值