Nginx的location路徑與proxy_pass匹配規(guī)則說明
路徑替換
配置proxy_pass時,可以實(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,錯誤的,也出現(xiàn)了雙斜杠
location /api {
proxy_pass http://127.0.0.1:5053/;
}- location、proxy_pass都加斜杠,實(shí)際代理地址:http://127.0.0.1:5053/test/list,錯誤的
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,錯誤的,少了一個斜桿
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)證請求時會校驗(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/;
}最后
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Nginx中l(wèi)ocation proxy_pass加與不加/的區(qū)別說明
- Nginx?location和proxy_pass配置示例詳解
- Nginx中proxy_pass的斜杠的兩種方式
- Nginx捕獲并自定義proxy_pass返回的錯誤問題
- 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)多個proxy_pass方式
- Nginx中proxy_pass使用小結(jié)
相關(guān)文章
nginx自定義變量與內(nèi)置預(yù)定義變量的使用
這篇文章主要介紹了nginx自定義變量與內(nèi)置預(yù)定義變量的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解
這篇文章主要介紹了nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Nginx配置之虛擬主機(jī)server_name優(yōu)先級方式
本文主要介紹了Nginx配置文件中不同匹配規(guī)則的優(yōu)先,包括精確匹配、以星號開頭的最長通配符、以星號結(jié)尾的最長通配符以及第一個正則表達(dá)式匹配,總結(jié)了Nginx處理匹配規(guī)則的優(yōu)先機(jī)制,以及注意事項(xiàng)2026-04-04
Linux平臺通過nginx和vsftpd構(gòu)建圖片服務(wù)器
這篇文章主要介紹了Linux平臺通過nginx和vsftpd構(gòu)建圖片服務(wù)器,需要的朋友可以參考下2017-05-05
詳解nginx 代理多個服務(wù)器(多個server方式)
本篇文章主要介紹了詳解nginx 代理多個服務(wù)器(多個server方式),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
nginx配置選項(xiàng)try_files的用法及說明
這篇文章主要介紹了nginx配置選項(xiàng)try_files的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-10-10

