nginx反向代理路徑匹配詳解
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
1、基本規(guī)則假如后端實(shí)際地址為:
則: 1)通過nginx轉(zhuǎn)發(fā),使用http://127.0.0.1/api/user/getById?id=123訪問
通過原有地址直接準(zhǔn)發(fā)非常簡單。 2)通過nginx轉(zhuǎn)發(fā),使用http://127.0.0.1/test/api/user/getById?id=123訪問
這里相當(dāng)于對請求添加了前綴,但在轉(zhuǎn)發(fā)的過程中是沒有前綴的,故需要去掉。關(guān)鍵點(diǎn)就是地址后面的 "/" 2.關(guān)于斜桿"/"的案例對比以服務(wù)地址http://127.0.0.1:8080/api/user/getById進(jìn)行說明,訪問地址是http://127.0.0.1/api/user/getById。location后斜桿與proxy_pass后斜桿問題如下: 1)location、proxy_pass都不加斜杠
實(shí)際代理地址:http://127.0.0.1:8080/api/user/getById。正確的 2)location加斜杠,proxy_pass不加斜杠
實(shí)際代理地址:http://127.0.0.1:8080/api/user/getById。正確的 3)location不加斜杠,proxy_pass加斜杠
實(shí)際代理地址:http://127.0.0.1:8080//user/getById。錯(cuò)誤的,也出現(xiàn)了雙斜杠 4)location、proxy_pass都加斜杠
實(shí)際代理地址:http://127.0.0.1:8080/user/getById 5)location不加斜杠,proxy_pass加"api"
實(shí)際代理地址:http://127.0.0.1:8080/api/user/getById。正確的 6)location加斜杠,proxy_pass加"api"
實(shí)際代理地址:http://127.0.0.1:8080/apiuser/getById。錯(cuò)誤的,少了一個(gè)斜桿 7)location不加斜杠,proxy_pass加"api/"
實(shí)際代理地址:http://127.0.0.1:8080/api//user/getById。這種情況會(huì)出現(xiàn)雙斜杠問題,后端在認(rèn)證請求時(shí)會(huì)校驗(yàn)失敗。 8)location加斜杠,proxy_pass加"api/"
實(shí)際代理地址:http://127.0.0.1:8080/api/user/getById。正確的 可以看出,兩者加不加斜桿的區(qū)別還是很大的,不同的場景使用不同的配置即可。簡單的,要么都不加,這樣轉(zhuǎn)發(fā)的地址是對應(yīng)的。 鏈接: https://www.cnblogs.com/szc1998/p/18137724閱讀原文:原文鏈接 該文章在 2025/7/2 0:37:00 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |