Nginx代理中使用斜杠的區(qū)別小結(jié)
代理地址是:http://127.0.0.1:8000
總結(jié):代理地址加斜杠替換,代理地址不加斜杠拼接
1、代理地址不加斜杠
# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/api/getInfo
location ^~/api/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際指向?yàn)椋篽ttp://127.0.0.1:8000/api/getInfo
location ^~/api {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}location定位的路徑設(shè)置,是否帶斜杠,沒有關(guān)系。
2、代理地址 + 斜杠
# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/getInfo
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000//getInfo
location ^~/api {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}如果代理地址加了斜杠,不管location是否加斜杠,api路徑都省略了。
3、代理地址 + 后綴
# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/wxgetInfo
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/wx;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/wx/getInfo
location ^~/api {
proxy_pass http://127.0.0.1:8000/wx;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}如果location帶斜杠,會省略url中的斜杠。
4、代理地址 + 后綴 + 斜杠
# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/wx/getInfo
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/wx/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}# 請求路徑為:http://127.0.0.1:8080/api/getInfo
# 實(shí)際代理為:http://127.0.0.1:8000/wx//getInfo
location ^~/api {
proxy_pass http://127.0.0.1:8000/wx/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}其他
精確匹配:server_name www.test.com ;
左側(cè)通配:server_name *.test.com ;
右側(cè)統(tǒng)配:server_name www.test.* ;
正則匹配:server_name ~^www\.test\.*$ ;
匹配優(yōu)先級:精確匹配 > 左側(cè)通配符匹配 > 右側(cè)通配符匹配 > 正則表達(dá)式匹配
#直接返回狀態(tài)碼
location / {
return 404;
}
#返回狀態(tài)碼 + 一段文本
location / {
return 404 "pages not found";
}
#返回狀態(tài)碼 + 重定向地址
location / {
return 302 /blog ;
}
#返回重定向地址
location / {
return https://www.test.com ;
}# http強(qiáng)制跳轉(zhuǎn)到https
server {
listen 80;
server_name test.com;
rewrite ^(.*)$ https://$server_name$1 permanent;
}到此這篇關(guān)于Nginx代理中使用斜杠的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)Nginx代理斜杠內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx將https協(xié)議反向代理到http協(xié)議請求上的實(shí)現(xiàn)
本文主要介紹了nginx將https協(xié)議反向代理到http協(xié)議請求上的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
Nginx配置文件nginx.conf的基本配置實(shí)例詳解
Nginx(engine x)是一個(gè)輕量級的高性能的HTTP和反向代理web服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,下面這篇文章主要給大家介紹了關(guān)于Nginx配置文件nginx.conf基本配置的相關(guān)資料,需要的朋友可以參考下2022-09-09
Apache和Nginx實(shí)現(xiàn)虛擬主機(jī)的3種方式小結(jié)
Apache是一個(gè)模型化的服務(wù)器,可以運(yùn)行在幾乎所有的服務(wù)器上。其屬于應(yīng)用服務(wù)器,這篇文章主要介紹了Apache和Nginx實(shí)現(xiàn)虛擬主機(jī)的3種方式,需要的朋友可以參考下2023-11-11
Nginx(自定義/預(yù)定義)變量,alias虛擬目錄解讀
這篇文章主要介紹了Nginx(自定義/預(yù)定義)變量,alias虛擬目錄,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-07-07

