[nginx] location proxy_pass 有无反斜杠各种效果

访问url不变:http://192.168.1.1/proxy/test.html

1.location /proxy   proxy_pass http://127.0.0.1
  http://127.0.0.1/proxy/test.html
2.location /proxy   proxy_pass http://127.0.0.1/
  http://127.0.0.1//test.html


3.location /proxy/  proxy_pass http://127.0.0.1
  http://127.0.0.1/proxy/test.html
4.location /proxy/  proxy_pass http://127.0.0.1/
  http://127.0.0.1/test.html


5.location /proxy   proxy_pass http://127.0.0.1/abc
  http://127.0.0.1/abc/test.html
6.location /proxy   proxy_pass http://127.0.0.1/abc/
  http://127.0.0.1/abc//test.html


7.location /proxy/  proxy_pass http://127.0.0.1/abc
  http://127.0.0.1/abctest.html
8.location /proxy/  proxy_pass http://127.0.0.1/abc/
  http://127.0.0.1/abc/test.html

瞎总结一下:

1.第2和第6实际的url会出现双斜杠,所以location匹配的时候,建议在URI最后加上斜杆;

2.看了官方文档proxy_pass的例子,只看到示例第3和第8,即location的URI最后要有斜杠;proxy_pass如果有URI,最后也要有斜杠。

  其实location和proxy_pass的URI最后面有无斜杠都可以,只要遵循官方的规则即可;

  a.proxy_pass有URI,proxy_pass的URI与location替换;

  b.proxy_pass无URI,请求中的URI原封不动传递到后端server。

3.个人建议,location的URI以斜杠结尾,proxy_pass若指定了URI,URI最后面也要跟上斜杠。

posted @ 2021-11-26 10:54  金枪语  阅读(536)  评论(0编辑  收藏  举报