nginx配置之proxy_pass路径加斜杠/以及包含路径的区别

nginx配置看似简单,但一些细节配置经常被大家忽略。

在proxy_pass中

1.proxy_pass包含路径如 http://127.0.0.1:8080/abc 和http://127.0.0.1:8080规则都有区别。

2.proxy_pass结尾加斜杠/和不加斜杠的有区别

下面四种情况分别用http://192.168.10.1/proxy/test.html 进行访问。

第一种(末尾加斜杠,proxy_pass中不包含路径):

location  /proxy/ {
proxy_pass http://127.0.0.1:81/;

}

结论:会被代理到http://127.0.0.1:81/test.html (proxy_pass+请求url匹配的location路径后的内容)

第二种(末尾不加斜杠,proxy_pass不包含路径)

location  /proxy/ {
proxy_pass http://127.0.0.1:81;

}

结论:会被代理到http://127.0.0.1:81/proxy/test.html (proxy_pass替换请求url的ip和端口)

第三种(末尾加斜杠,proxy_pass包含路径):

location  /proxy/ {
proxy_pass http://127.0.0.1:81/abc/;

}

结论:会被代理到http://127.0.0.1:81/abc/test.html (proxy_pass+请求url匹配的location路径后的内容)

第四种(末尾不加斜杠,url包含路径):

location  /proxy/ {
proxy_pass http://127.0.0.1:81/abc;

}

结论:会被代理到http://127.0.0.1:81/abctest.html (proxy_pass+请求url匹配的location路径后的内容)

总结:

1.如果proxy_pass后面有斜杠。转发url为proxy_pass+原url匹配的location路径之后的内容。

     例:原请求http://192.168.10.1/proxy/test.html,  location 为/proxy/

      proxy_pass为 http://127.0.0.1:81/abc/

     转发路径:(proxy_pass)http://127.0.0.1:81/abc/加上原请求部分路径test.html,最终路径http://127.0.0.1:81/abc/test.html

2.proxy_pass后面没有斜杠,

  a.只有当proxy_pass只有IP加端口,无路径时。匹配规则为proxy_pass替换原请求url的ip和端口,

   同时保留了location路径。例子为上述的第二种情况。

   b.当proxy_pass端口后包含路径时,匹配规则同1.

3.推荐:一般建议proxy_pass后面不包含路径
通过查看tomcat或者接受请求的服务器方日志,我们可以看到每次请求的后端的uri完整的值,进行核对

原文链接:https://blog.csdn.net/u010786902/article/details/91414747

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值