Nginx中proxy_pass末尾帶斜杠/和不帶的區(qū)別
nginx的代理的路徑經(jīng)常會讓人摸不著頭腦,所以打算整理一下該篇筆記,介紹nginx配置文件中proxy_pass路徑后帶/與不帶/的區(qū)別,方便在自己需要時進行閱讀查閱。
假如要將8080端口上的請求轉(zhuǎn)發(fā)至3000端口。
以3000端口為例,編寫proxy_pass有兩種形式。
- 無斜桿:
http://localhost:3000 - 有斜桿:
http://localhost:3000/
假設(shè)前端請求為http://localhost:8080/get/test。
我們暫且把/get/test稱為請求部分。
不帶 "/'
server {
listen 8080;
server_name localhost;
location /get {
proxy_pass http://localhost:3000;
}
#或者
location /get/ {
proxy_pass http://localhost:3000;
}
#結(jié)果都是 將http://localhost:8080/get/test轉(zhuǎn)發(fā)去http://localhost:3000/get/test
}
proxy_pass:http://localhost:3000。
無斜桿location匹配到的部分也屬于請求的部分。
location無論用/get還是用/get/只要匹配上之后都會將整個請求部分/get/test加到proxy_pass上。
http://localhost:3000+/get/test等于請求http://localhost:3000/get/test。
帶 "/"
server {
listen 8080;
server_name localhost;
location /get {
# 結(jié)果是 將http://localhost:8080/get/test轉(zhuǎn)發(fā)去http://localhost:3000//test,出錯~
proxy_pass http://localhost:3000/;
}
#或者
location /get/ {
# 結(jié)果是 將http://localhost:8080/get/test轉(zhuǎn)發(fā)去http://localhost:3000/test
proxy_pass http://localhost:3000/;
}
}
proxy_pass:http://localhost:3000/。
有斜桿location匹配到的部分只用于匹配,不屬于請求部分,需要在請求部分將location匹配到的部分剔除。
location用/get則是http://localhost:3000/+(/get/test -/get)等于請求http://localhost:3000//test
location用/get/則是http://localhost:3000/+(/get/test -/get/)等于請求http://localhost:3000/test
"/" 后面還有路徑信息
server {
listen 8080;
server_name localhost;
location /get {
# 結(jié)果是 將http://localhost:8080/get/test轉(zhuǎn)發(fā)去http://localhost:3000/abc/test
proxy_pass http://localhost:3000/abc;
}
#或者
location /get/ {
# 結(jié)果是 將http://localhost:8080/get/test轉(zhuǎn)發(fā)去http://localhost:3000/abctest,出錯~
proxy_pass http://localhost:3000/abc;
}
}
proxy_pass:http://localhost:3000/abc。
同有斜桿的規(guī)則,在請求部分剔除location后加在上面即可。
location用/get則是http://localhost:3000/abc+(/get/test -/get)等于請求http://localhost:3000/abc/test
location用/get/則是http://localhost:3000/abc+(/get/test -/get/)等于請求http://localhost:3000/abctest
總結(jié)
- 不帶 / 時,可以理解為簡單的全路徑拼接,不作任何處理
- 帶 / 時, proxy_pass + (原路徑 - 匹配規(guī)則)
到此這篇關(guān)于Nginx中proxy_pass末尾帶斜杠/和不帶的區(qū)別的文章就介紹到這了,更多相關(guān)Nginx中proxy_pass斜杠/內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx實現(xiàn)https網(wǎng)站配置代碼實例
這篇文章主要介紹了Nginx實現(xiàn)https網(wǎng)站配置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
詳解nginx服務(wù)器綁定域名和設(shè)置根目錄的方法
這篇文章主要介紹了詳解nginx服務(wù)器綁定域名和設(shè)置根目錄的方法,nginx服務(wù)器綁定域名以及設(shè)置根目錄非常方便,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Nginx設(shè)置wordpress偽靜態(tài)的方法示例
偽靜態(tài)是相對真實靜態(tài)來講的,通常我們?yōu)榱嗽鰪娝阉饕娴挠押妹?,這篇文章主要介紹了Nginx設(shè)置wordpress偽靜態(tài)的方法示例,非常具有實用價值,需要的朋友可以參考下2018-09-09
解讀nginx -s reload會產(chǎn)生什么影響
這篇文章主要介紹了nginx -s reload會產(chǎn)生什么影響,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-07-07
通過nginx反向代理來調(diào)試代碼的實現(xiàn)
這篇文章主要介紹了通過nginx反向代理來調(diào)試代碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

