Nginx配置转发

nginx location proxy_pass 后面的url 加与不加/的区别

 在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

 首先是location进行的是模糊匹配

1)没有“/”时,location /abc/def可以匹配/abc/defghi请求,也可以匹配/abc/def/ghi等
2)而有“/”时,location /abc/def/不能匹配/abc/defghi请求,只能匹配/abc/def/anything这样的请求

下面四种情况分别用http://luxingda.top/py/test 进行访问。

第一种:

location  /py/ {

proxy_pass http://127.0.0.1:5000/;

}

结论:会被代理到http://127.0.0.1:5000/test  这个url

第二种(相对于第一种,最后少一个 /)

location  /py/ {

proxy_pass http://127.0.0.1:5000
; }

 

结论:会被代理到http://127.0.0.1:5000/py/test这个url

第三种:

location  /py/ {

proxy_pass http://127.0.0.1:5000/py/;

}

结论:会被代理到http://127.0.0.1:5000/py/test这个url。

第四种(相对于第三种,最后少一个 / ):

location  /py/ {

proxy_pass http://127.0.0.1:5000/py;

}

结论:会被代理到http://127.0.0.1:5000/pytest这个url

 

posted @ 2019-12-25 15:48  对我有点小自信  阅读(51792)  评论(1编辑  收藏  举报