解讀nginx反向代理location和proxy_pass的映射關(guān)系
配置nginx反向代理時(shí),總是需要嘗試多次才能配置成功,通過本文做個(gè)記錄,方便以后查看。
1. proxy_pass 只有主機(jī)地址時(shí)
只有主機(jī)地址的意思是,proxy_pass
值為 http://host:port
這種形式,url部分沒有 /
或 /xxx
等.
對(duì)應(yīng)到本文示例就是 http://backend
。
這種情況下,相當(dāng)于下面的公式:
backend url = proxy_pass + path
這種情況下,請(qǐng)求的 path 部分會(huì)直接追加到 proxy_pass 地址后,相當(dāng)于把nginx地址一對(duì)一的映射到了后端地址,這種配置方式理解最簡(jiǎn)單。
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/ | http://backend | / | http://nginx/ | http://backend/ |
/ | http://backend | /hello | http://nginx/hello | http://backend/hello |
/ | http://backend | /hello/world | http://nginx/hello/world | http://backend/hello/world |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/a | http://backend | /a | http://nginx/a | http://backend/a |
/a | http://backend | /ab | http://nginx/ab | http://backend/ab |
/a | http://backend | /a/ | http://nginx/a/ | http://backend/a/ |
/a | http://backend | /a/b | http://nginx/a/b | http://backend/a/b |
/a | http://backend | /a/b/ | http://nginx/a/b/ | http://backend/a/b/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/b/ | http://backend | /b/ | http://nginx/b/ | http://backend/b/ |
/b/ | http://backend | /b/a | http://nginx/b/a | http://backend/b/a |
/b/ | http://backend | /b/a/ | http://nginx/b/a/ | http://backend/b/a/ |
2. proxy_pass 帶路徑時(shí)
這種情況下,相當(dāng)于下面的公式:
backend url = proxy_pass + (path - location)
path 部分減去匹配的 location 部分后,剩余內(nèi)容追加到 proxy_pass 上去請(qǐng)求。
如果不是為了 /u
去匹配 /uv
這種路徑,使用 /v/
去匹配 /v/w
這種更準(zhǔn)確,不會(huì)出現(xiàn) //
這種情況。
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/u | http://backend/ | /u | http://nginx/u | http://backend/ |
/u | http://backend/ | /uv | http://nginx/uv | http://backend/v |
/u | http://backend/ | /u/ | http://nginx/u/ | http://backend// |
/u | http://backend/ | /u/v | http://nginx/u/v | http://backend//v |
/u | http://backend/ | /u/v/ | http://nginx/u/v/ | http://backend//v/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/v/ | http://backend/ | /v/ | http://nginx/v/ | http://backend/ |
/v/ | http://backend/ | /v/w | http://nginx/v/w | http://backend/w |
/v/ | http://backend/ | /v/w/ | http://nginx/v/w/ | http://backend/w/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/c | http://backend/c | /c | http://nginx/c | http://backend/c |
/c | http://backend/c | /cd | http://nginx/cd | http://backend/cd |
/c | http://backend/c | /c/ | http://nginx/c/ | http://backend/c/ |
/c | http://backend/c | /c/d | http://nginx/c/d | http://backend/c/d |
/c | http://backend/c | /c/d/ | http://nginx/c/d/ | http://backend/c/d/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/d/ | http://backend/d | /d/ | http://nginx/d/ | http://backend/d |
/d/ | http://backend/d | /d/e | http://nginx/d/e | http://backend/de |
/d/ | http://backend/d | /d/e/ | http://nginx/d/e/ | http://backend/de/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/e | http://backend/e/ | /e | http://nginx/e | http://backend/e/ |
/e | http://backend/e/ | /ef | http://nginx/ef | http://backend/e/f |
/e | http://backend/e/ | /e/ | http://nginx/e/ | http://backend/e// |
/e | http://backend/e/ | /e/f | http://nginx/e/f | http://backend/e//f |
/e | http://backend/e/ | /e/f/ | http://nginx/e/f/ | http://backend/e//f/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/f/ | http://backend/f/ | /f/ | http://nginx/f/ | http://backend/f/ |
/f/ | http://backend/f/ | /f/g | http://nginx/f/g | http://backend/f/g |
/f/ | http://backend/f/ | /f/g/ | http://nginx/f/g/ | http://backend/f/g/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/g | http://backend/m | /g | http://nginx/g | http://backend/m |
/g | http://backend/m | /gh | http://nginx/gh | http://backend/mh |
/g | http://backend/m | /g/ | http://nginx/g/ | http://backend/m/ |
/g | http://backend/m | /g/h | http://nginx/g/h | http://backend/m/h |
/g | http://backend/m | /g/h/ | http://nginx/g/h/ | http://backend/m/h/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/h/ | http://backend/n | /h/ | http://nginx/h/ | http://backend/n |
/h/ | http://backend/n | /h/i | http://nginx/h/i | http://backend/ni |
/h/ | http://backend/n | /h/i/ | http://nginx/h/i/ | http://backend/ni/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/i | http://backend/x/ | /i | http://nginx/i | http://backend/x/ |
/i | http://backend/x/ | /ij | http://nginx/ij | http://backend/x/j |
/i | http://backend/x/ | /i/ | http://nginx/i/ | http://backend/x// |
/i | http://backend/x/ | /i/j | http://nginx/i/j | http://backend/x//j |
/i | http://backend/x/ | /i/j/ | http://nginx/i/j/ | http://backend/x//j/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/j/ | http://backend/y/ | /j/ | http://nginx/j/ | http://backend/y/ |
/j/ | http://backend/y/ | /j/k | http://nginx/j/k | http://backend/y/k |
/j/ | http://backend/y/ | /j/k/ | http://nginx/j/k/ | http://backend/y/k/ |
location | proxy_pass | path | nginx url | backend url |
---|---|---|---|---|
/ws/ | http://backend/spring/ws/ | /ws/qrlogin | http://nginx/ws/qrlogin | http://backend/spring/ws/qrlogin |
/ws/ | http://backend/spring/ws/ | /ws/stomp | http://nginx/ws/stomp | http://backend/spring/ws/stomp |
參考文檔:
英文 - ngx_http_proxy_module.html#proxy_pass
中文 - ngx_http_proxy_module#proxy_pass
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx實(shí)現(xiàn)TCP和UDP代理的方法步驟
Nginx 1.9.13 及以上版本支持TCP/UDP代理功能,通過配置監(jiān)聽端口、后端服務(wù)器地址等參數(shù),實(shí)現(xiàn)客戶端請(qǐng)求的轉(zhuǎn)發(fā)和響應(yīng)的返回,下面就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2024-12-12Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Requ
HTTP 413 Request Entity Too Large錯(cuò)誤常常出現(xiàn)在客戶端發(fā)送的請(qǐng)求體超過服務(wù)器允許的大小限制時(shí),本文主要介紹了Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯(cuò)誤解決,感興趣的可以了解一下2024-07-07nginx worker進(jìn)程循環(huán)的實(shí)現(xiàn)
這篇文章主要介紹了nginx worker進(jìn)程循環(huán)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02nginx編譯安裝出現(xiàn)的常見錯(cuò)誤及解決方法
這篇文章給大家介紹了nginx在編譯安裝過程中容易出現(xiàn)的常見錯(cuò)誤以及解決方法,文中有詳細(xì)的代碼講解,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08