Nginx的location路徑與proxy_pass匹配規(guī)則說明
路徑替換
配置proxy_pass時(shí),可以實(shí)現(xiàn)URL路徑的部分替換。
proxy_pass的目標(biāo)地址,默認(rèn)不帶/,表示只代理域名(ip+端口),path和query部分不會變(把請求的path和query拼接到proxy_pass目標(biāo)域名之后作為代理的URL)
如果在目標(biāo)地址端口后有‘/’或者‘/xx/yy’等目錄,則表示把path中l(wèi)ocation匹配成功的部分剪切掉之后再拼接到proxy_pass目標(biāo)地址后面
- 比如請求 /a/b.html
location /a { proxy_pass http://server; }
- 實(shí)際代理的目標(biāo)url是http://server/a/b.html (把/a/b.html拼接到http://server之后)
location /a/ { proxy_pass http://server/; }
總結(jié)
當(dāng)訪問地址是 http://127.0.0.1/api/user/list
若proxy_pass代理地址端口后無任何字符
則轉(zhuǎn)發(fā)后地址為:代理地址+訪問的path
location | proxy_pass | 代理路徑 |
---|---|---|
/api/ | http://server:8080 | http://server:8080/api/user/list |
/api/ | http://server:8080/ | http://server:8080/user/list |
/api | http://server:8080 | http://server:8080/api/user/list |
/api | http://server:8080/ | http://server:8080//user/list |
若proxy_pass代理地址端口后有目錄(包括"/")
則轉(zhuǎn)發(fā)后地址為:代理地址+訪問的path去除location匹配的路徑
location | proxy_pass | 代理路徑 |
---|---|---|
/api/ | http://server:8080/gw | http://server:8080/gwuser/list |
/api/ | http://server:8080/gw/ | http://server:8080/gw/user/list |
/api | http://server:8080/gw | http://server:8080/gw/user/list |
/api | http://server:8080/gw/ | http://server:8080/gw//user/list |
注意
location后斜桿與proxy_pass后斜桿"/"問題,最好要么兩者都加斜桿,要么都不加
以服務(wù)地址http://127.0.0.1:5053/api/test/list進(jìn)行說明,訪問地址是http://127.0.0.1/api/test/list。
location后斜桿與proxy_pass后斜桿問題如下:
- location、proxy_pass都不加斜杠,實(shí)際代理地址:http://127.0.0.1:5053/api/test/list,正確的
location /api { proxy_pass http://127.0.0.1:5053; }
- location加斜杠,proxy_pass不加斜杠,實(shí)際代理地址:http://127.0.0.1:5053/api/test/list,正確的
location /api/ { proxy_pass http://127.0.0.1:5053; }
- location不加斜杠,proxy_pass加斜杠,實(shí)際代理地址:http://127.0.0.1:5053//test/list,錯(cuò)誤的,也出現(xiàn)了雙斜杠
location /api { proxy_pass http://127.0.0.1:5053/; }
- location、proxy_pass都加斜杠,實(shí)際代理地址:http://127.0.0.1:5053/test/list,錯(cuò)誤的
location /api/ { proxy_pass http://127.0.0.1:5053/; }
- location不加斜杠,proxy_pass加"api",實(shí)際代理地址:http://127.0.0.1:5053/api/test/list,正確的
location /api { proxy_pass http://127.0.0.1:5053/api; }
- location加斜杠,proxy_pass加"api",實(shí)際代理地址:http://127.0.0.1:5053/apitest/list,錯(cuò)誤的,少了一個(gè)斜桿
location /api/ { proxy_pass http://127.0.0.1:5053/api; }
- location不加斜杠,proxy_pass加"api/",實(shí)際代理地址:http://127.0.0.1:5053/api//test/list,出現(xiàn)雙斜杠問題,后端在認(rèn)證請求時(shí)會校驗(yàn)失敗
location /api { proxy_pass http://127.0.0.1:5053/api/; }
- location加斜杠,proxy_pass加"api/",實(shí)際代理地址:http://127.0.0.1:5053/api/test/list,正確的
location /api/ { proxy_pass http://127.0.0.1:5053/api/; }
最后
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Nginx中l(wèi)ocation proxy_pass加與不加/的區(qū)別說明
- Nginx?location和proxy_pass配置示例詳解
- Nginx中proxy_pass的斜杠的兩種方式
- Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題
- nginx中如何配置proxy_pass
- Nginx rewrite和proxy_pass的區(qū)別及說明
- Nginx proxy_pass如何到https后端
- nginx代理參數(shù)proxy_pass的實(shí)現(xiàn)
- nginx反向代理proxy_pass遇到的死循環(huán)問題
- 解決nginx配置proxy_pass之后,響應(yīng)變慢的問題
- Nginx使用if指令實(shí)現(xiàn)多個(gè)proxy_pass方式
- Nginx中proxy_pass使用小結(jié)
相關(guān)文章
Nginx隱藏server頭信息的實(shí)現(xiàn)
本文主要介紹了Nginx隱藏server頭信息的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Nginx配置真實(shí)IP地址轉(zhuǎn)發(fā)的實(shí)現(xiàn)示例
在許多web應(yīng)用程序中,獲取客戶端的真實(shí) IP 地址非常重要,本文就來介紹一下Nginx配置真實(shí)IP地址轉(zhuǎn)發(fā)的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-11-11nginx 反向代理之 proxy_pass的實(shí)現(xiàn)
這篇文章主要介紹了nginx 反向代理之 proxy_pass的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Nginx配置反向代理服務(wù)器實(shí)現(xiàn)在https網(wǎng)站中請求http資源
?Nginx反向代理?是一種將客戶端請求轉(zhuǎn)發(fā)到后端服務(wù)器的技術(shù),主要用于負(fù)載均衡、提高安全性和提升性能,本文給大家介紹了Nginx配置反向代理服務(wù)器實(shí)現(xiàn)在https網(wǎng)站中請求http資源,需要的朋友可以參考下2025-03-03Nginx報(bào)403 forbidden錯(cuò)誤 (13: Permission denied)的解決辦法
這篇文章主要介紹了Nginx報(bào)403 forbidden錯(cuò)誤 (13: Permission denied)的解決辦法,引起nginx 403 forbidden通常是三種情況:一是缺少索引文件,二是權(quán)限問題,三是SELinux狀態(tài)。具體內(nèi)容詳情大家可以參考下本文2017-01-01詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法
本篇文章主要介紹了詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01nginx-ingress-controller部署配置詳解
這篇文章主要為大家介紹了nginx ingress controller的部署配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11