Nginx反向代理location和proxy_pass配置規(guī)則詳細(xì)總結(jié)
一、location配置規(guī)則
1.匹配模式及順序舉例
location = /uri | = 開(kāi)頭表示精確匹配,只有完全匹配上才能生效 |
location ^~ /uri | ^~ 開(kāi)頭對(duì) URL 路徑進(jìn)行前綴匹配,并且在正則之前 |
location ~ pattern | ~ 開(kāi)頭表示區(qū)分大小寫(xiě)的正則匹配 |
location /uri | 不帶任何修飾符,也表示前綴匹配,但是在正則匹配之后,如果沒(méi)有正則命中,命中最長(zhǎng)的規(guī)則 |
location / | 通用匹配,任何未匹配到其它 location 的請(qǐng)求都會(huì)匹配到,相當(dāng)于 switch 中的 default |
2.location 是否以“/”結(jié)尾
在 ngnix 中 location 進(jìn)行的是模糊匹配
- 沒(méi)有“/”結(jié)尾時(shí),location /abc/def 可以匹配 /abc/defghi 請(qǐng)求,也可以匹配 /abc/def/ghi 等
- 而有“/”結(jié)尾時(shí),location /abc/def/ 不能匹配 /abc/defghi 請(qǐng)求,只能匹配 /abc/def/anything 這樣的請(qǐng)求
二、proxy_pass配置規(guī)則
(1)配置 proxy_pass 時(shí),當(dāng)在后面的 url 加上了 /,相當(dāng)于是絕對(duì)路徑,則 Nginx 不會(huì)把 location 中匹配的路徑部分加入代理 uri。
(2)如果配置 proxy_pass 時(shí),后面沒(méi)有 /,Nginx 則會(huì)把匹配的路徑部分加入代理 uri。
例如:
server { listen 8081; server_name localhost; location / { root html; index index.html index.htm; } #情景1:proxy_pass后有/ ,表絕對(duì)路徑,不把匹配部分加入最終代理路徑(location 和proxy_pass結(jié)尾一致) #訪問(wèn)地址:http://localhost:8081/WCP.Service/wcp/modeladapter/download/asc.shtml #最終代理:http://10.194.171.7:13082/modeladapter/download/asc.shtml location /WCP.Service/wcp/modeladapter/download/ { proxy_pass http://10.194.171.7:13082/modeladapter/download/; } #訪問(wèn)地址:http://localhost:8081/model/asc.shtml #最終代理:http://127.0.0.1:8082/model/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/model/; } #情景2:proxy_pass后有/ ,表絕對(duì)路徑,不把匹配部分加入最終代理路徑(location 和proxy_pass結(jié)尾不一致) #訪問(wèn)地址:http://localhost:8081/model/asc.shtml #最終代理:http://127.0.0.1:8082/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/; } #情景3:proxy_pass后沒(méi)有 / ,Nginx會(huì)把匹配部分帶到代理的url #訪問(wèn)地址:http://localhost:8081/model/asc.shtml #最終代理:http://127.0.0.1:8082/model/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082; } #情景4 #訪問(wèn)地址:http://localhost:8081/model/asc.shtml #最終代理:http://127.0.0.1:8082/AAAmodel/asc.shtml location /model/ { proxy_pass http://127.0.0.1:8082/AAA; } #情景5 #訪問(wèn)地址:http://localhost:8081/model/asc.shtml #最終代理:http://127.0.0.1:8082/asc.shtml location /model { proxy_pass http://127.0.0.1:8082/; } #情景6 #訪問(wèn)地址:http://localhost:8081/modelBBB/asc.shtml #最終代理:http://127.0.0.1:8082/asc.shtml location /model { proxy_pass http://127.0.0.1:8082/; } location /opus-front-sso { proxy_pass http://10.194.170.94/opus-front-sso; } location /awater { proxy_pass http://10.194.170.94/awater; } }
補(bǔ)充:Nginx配置proxy_pass轉(zhuǎn)發(fā)的/路徑問(wèn)題
在nginx中配置proxy_pass時(shí),如果是按照^~匹配路徑時(shí),要注意proxy_pass后的url最后的/,當(dāng)加上了/,相當(dāng)于是絕對(duì)根路徑,則nginx不會(huì)把location中匹配的路徑部分代理走;如果沒(méi)有/,則會(huì)把匹配的路徑部分也給代理走。
location ^~ /static_js/ { proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com/; }
如上面的配置,如果請(qǐng)求的url是http://servername/static_js/test.html
會(huì)被代理成http://js.test.com/test.html
而如果這么配置
location ^~ /static_js/ { proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com; }
則會(huì)被代理到http://js.test.com/static_js/test.htm
總結(jié)
到此這篇關(guān)于Nginx反向代理location和proxy_pass配置規(guī)則的文章就介紹到這了,更多相關(guān)Nginx location和proxy_pass配置規(guī)則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Nginx的location的常見(jiàn)規(guī)則優(yōu)先級(jí)問(wèn)題
- nginx 配置location匹配規(guī)則實(shí)例講解
- Nginx Location指令URI匹配規(guī)則詳解小結(jié)
- Nginx location匹配規(guī)則的方法示例
- nginx配置location總結(jié)location正則寫(xiě)法及rewrite規(guī)則寫(xiě)法
- 詳解nginx配置location總結(jié)及rewrite規(guī)則寫(xiě)法
- 詳解Nginx location 匹配規(guī)則
- Nginx服務(wù)器的location指令匹配規(guī)則詳解
- nginx中的路徑匹配location規(guī)則詳解
相關(guān)文章
nginx配置完rewrite瀏覽器提示將您重定向的次數(shù)過(guò)多的解決方法
本文主要介紹了nginx配置完rewrite瀏覽器提示將您重定向的次數(shù)過(guò)多的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Nginx配置ssl證書(shū)(https)的全過(guò)程
這篇文章主要介紹了Nginx配置ssl證書(shū)(https)的過(guò)程,在文中大家需要特別注意,如果有防火墻的話,記得開(kāi)通443端口,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10編寫(xiě)Go程序?qū)ginx服務(wù)器進(jìn)行性能測(cè)試的方法
這篇文章主要介紹了編寫(xiě)Go程序?qū)ginx服務(wù)器進(jìn)行性能測(cè)試的方法,包括對(duì)其負(fù)載均衡和緩存等方面的測(cè)試,極力推薦!需要的朋友可以參考下2015-06-06Nginx對(duì)網(wǎng)段內(nèi)ip的連接數(shù)限流配置詳解
這篇文章主要介紹了Nginx對(duì)網(wǎng)段內(nèi)ip的連接數(shù)限流配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03nginx的請(qǐng)求轉(zhuǎn)發(fā)配置過(guò)程
Nginx在Windows和Linux環(huán)境下的安裝、啟動(dòng)、停止、配置和請(qǐng)求轉(zhuǎn)發(fā)過(guò)程,配置文件語(yǔ)法檢測(cè)、優(yōu)雅關(guān)閉、熱部署和日志文件重新打開(kāi),配置多個(gè)服務(wù)的請(qǐng)求轉(zhuǎn)發(fā)規(guī)則,修改前端API地址,設(shè)置最大上傳文件大小2024-12-12Nginx編譯參數(shù)大全 configure參數(shù)中文詳解
這篇文章主要介紹了Nginx編譯參數(shù)大全,Nginx configure參數(shù)中文詳解,需要的朋友可以參考下2014-04-04