網(wǎng)站首頁 編程語言 正文
一、location配置規(guī)則
1.匹配模式及順序舉例
location = /uri? | =?開頭表示精確匹配,只有完全匹配上才能生效 |
location ^~ /uri? | ^~?開頭對?URL?路徑進行前綴匹配,并且在正則之前 |
location ~ pattern | ~?開頭表示區(qū)分大小寫的正則匹配 |
location /uri | 不帶任何修飾符,也表示前綴匹配,但是在正則匹配之后,如果沒有正則命中,命中最長的規(guī)則 |
location / | 通用匹配,任何未匹配到其它?location?的請求都會匹配到,相當于?switch?中的?default |
2.location 是否以“/”結(jié)尾
在 ngnix 中 location 進行的是模糊匹配
- 沒有“/”結(jié)尾時,location /abc/def 可以匹配 /abc/defghi 請求,也可以匹配 /abc/def/ghi 等
- 而有“/”結(jié)尾時,location /abc/def/ 不能匹配 /abc/defghi 請求,只能匹配 /abc/def/anything 這樣的請求
二、proxy_pass配置規(guī)則
(1)配置 proxy_pass 時,當在后面的 url 加上了 /,相當于是絕對路徑,則 Nginx 不會把 location 中匹配的路徑部分加入代理 uri。
(2)如果配置 proxy_pass 時,后面沒有 /,Nginx 則會把匹配的路徑部分加入代理 uri。
例如:
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#情景1:proxy_pass后有/ ,表絕對路徑,不把匹配部分加入最終代理路徑(location 和proxy_pass結(jié)尾一致)
#訪問地址: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/;
}
#訪問地址: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后有/ ,表絕對路徑,不把匹配部分加入最終代理路徑(location 和proxy_pass結(jié)尾不一致)
#訪問地址: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后沒有 / ,Nginx會把匹配部分帶到代理的url
#訪問地址: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
#訪問地址: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
#訪問地址: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
#訪問地址: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;
}
}
補充:Nginx配置proxy_pass轉(zhuǎn)發(fā)的/路徑問題
在nginx中配置proxy_pass時,如果是按照^~匹配路徑時,要注意proxy_pass后的url最后的/,當加上了/,相當于是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分也給代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果請求的url是http://servername/static_js/test.html
會被代理成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;
}
則會被代理到http://js.test.com/static_js/test.htm
總結(jié)
原文鏈接:https://blog.csdn.net/qq_36528215/article/details/123570962
相關(guān)推薦
- 2023-05-24 Golang實現(xiàn)AES對稱加密算法實例詳解_Golang
- 2023-04-27 解讀react的onClick自動觸發(fā)等相關(guān)問題_React
- 2022-10-11 Data truncation: Data too long for column ‘context
- 2022-05-02 詳解在Python中使用OpenCV進行直線檢測_python
- 2022-05-08 jquery實現(xiàn)淘寶詳情頁選擇套餐_jquery
- 2022-11-30 Android?Binder?通信原理圖文詳解_Android
- 2022-06-24 python類名和類方法cls修改類變量的值_python
- 2022-07-19 element-ui根據(jù)條件合并單元格
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支