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