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后面不包含路徑

轉自:https://blog.csdn.net/u010786902/article/details/91414747


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM