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后面不包含路徑
轉自:https://blog.csdn.net/u010786902/article/details/91414747