網(wǎng)站首頁 編程語言 正文
前置文章:
Linux(CentOS7) 下 Nginx1.15.8 安裝步驟
Nginx 的配置文件 nginx.conf
我們訪問一個網(wǎng)址,服務(wù)器返回對應(yīng)的資源。那么一個網(wǎng)址是如何對應(yīng)一個資源的呢?
用 Nginx 可以很好地幫我們實現(xiàn)路由功能,我們所有需要做的就是配置好 location 模塊。
語法規(guī)則
?location [=|~|~*|^~] /uri/ {… }
符號 | 含義 |
= | 精確匹配? |
^~ | 非正則匹配 |
~ | 正則匹配(區(qū)分大小寫) |
~* | 正則匹配(不區(qū)分大小寫) |
!~ | 正則不匹配(區(qū)分大小寫) |
!~* | 正則不匹配(不區(qū)分大小寫) |
? | 普通匹配(這里沒有符號的時候) |
匹配規(guī)則
1. 精準匹配命中時,停止location
2.一般匹配(普通和非正則)命中時,對比所有命中的一般匹配,選出最長的一條
3.如果最長的那一條為非正則匹配,直接匹配此條,停止location
4.如果最長的那一條為普通匹配,繼續(xù)嘗試正則location(以上至此都不存在代碼順序)
5.按代碼順序執(zhí)行正則匹配,當?shù)谝粭l正則location命中時,停止location
示例:
想運行以下示例需先下載第三方模塊echo-nginx-module:
#下載到/usr/local/src目錄
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
tar -zxvf v0.61.tar.gz ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ??
#在Nginx源碼目錄nginx-1.15.8下配置,--add-module指向模塊目錄即會安裝插件到nginx中
./configure --add-module=/usr/local/src/echo-nginx-module-0.61/
make && make install
#驗證安裝
nginx -V
你還需要配置 C:\Windows\System32\drivers\etc\hosts 文件,添加“虛擬機ip 域名”到最后:
192.168.100.14 test.loaction.com
server {
listen 80;
server_name test.location.com;
#精準匹配測試
#第1,2條雖然匹配,但第三條是精準匹配,出第三條結(jié)果
#測試路徑/equal/a/b/c
location ~ /equal/* {
echo '/equal/*';
}
location /equal/a/b {
echo '/equal/a/b';
}
location = /equal/a/b/c {
echo '/equal/a/b/c';
}
#普通匹配測試
#第1,2條雖然匹配,第三條匹配更長,出第三條結(jié)果
#測試路徑/match/a/b/c
location /match/a {
return 200 "/match/a";
}
location /match/a/b {
return 200 "/match/a/b";
}
location /match/a/b/c {
return 200 "/match/a/b/c";
}
location /match/a/b/c/d {
return 200 "/match/a/b/c/d";
}
#正則匹配覆蓋普通匹配,不會覆蓋非正則匹配
#訪問/re/a.htm,會被后面的正則覆蓋
#訪問/re/a/b開頭的路徑,不會被后面的正則覆蓋
location /re/a.htm {
echo 'match /re/a.htm';
}
location ^~ /re/a/b {
echo 'math ^~/re/a/b*';
}
location ~ /re/(.*)\.(htm|js|css)$ {
echo "cover /re/$1.$2";
}
#正則匹配成功一條后,便不再走其它正則
#測試路徑/rex/a/b/c.htm
location ~ /rex/.*\.(htm|js|css)$ {
echo "match first";
}
location ~ /rex/a/(.*)\.(htm|js|css)$ {
echo "match second";
}
location ~ /rex/a/b/(.*)\.(htm|js|css)$ {
echo "match third";
}
}
結(jié)果:
精準匹配最優(yōu)先:
一般匹配選最長:
正則覆蓋普通匹:
不能覆蓋非正則
正則匹配選首位:
proxy_pass 代理轉(zhuǎn)發(fā)
在上面的示例中,我們通過修改本機hosts文件,讓本機訪問?test.loaction.com 相當于訪問 192.168.100.14。我們現(xiàn)在假設(shè)有一個服務(wù)在?192.168.100.15 這臺機器上。我們能不能通過訪問?test.loaction.com,訪問到?192.168.100.15 上的資源呢?
這樣的想法其實是在模擬實際做項目的時候,我們可以只購買一個域名,然后訪問多個云服務(wù)器。我們要做的就是讓域名對應(yīng)的服務(wù)器具有代理轉(zhuǎn)發(fā)的功能。這里是服務(wù)器作為代理,讓客戶端能通過訪問代理服務(wù)器來訪問其它服務(wù)器,所以是反向代理。
通過在 location 里配置 proxy_pass 就能實現(xiàn)代理轉(zhuǎn)發(fā)功能,幫助我們將請求轉(zhuǎn)發(fā)到別的服務(wù)器。
我們先看示例再看規(guī)則:
server {
listen 80;
server_name test.location.com;
本機ip地址:192.168.100.104
#后端服務(wù)路徑:
http://192.168.100.105:8080/user/query?id=1
#規(guī)則一:
#訪問路徑:http://test.location.com/user/query?id=1
location /user {
#path1:/user path2:/query
#ip:port 后面無 /
proxy_pass http://192.168.0.105:8080;
}
規(guī)則二:
#訪問路徑 :http://test.location.com/A/user/query?id=1
location /A/user {
#path1:/A/user path2:/query
#ip:port 后面有 /xxx
proxy_pass http://192.168.0.105:8080/user;
}
}
解讀:
后端服務(wù)路徑都是?http://192.168.100.105:8080/user/query?id=1。用規(guī)則一,可以使訪問路徑為?http://test.location.com/user/query?id=1;用規(guī)則二,可以使訪問路徑為?http://test.location.com/A/user/query?id=1。
規(guī)則:
1. 訪問路徑格式為 sever_name + path1 + path2 + ?param
2. location 后面的路徑就是 path1,對照訪問路徑格式,path1 和 ?param 之間的為path2
3. 如果 proxy_pass 后面的值形如 http: // ip : port;?那么實際訪問的地址是?http: // ip : port + path1 + path2 +??param
4.?如果 proxy_pass 后面的值形如 http: // ip : port / xxx; (末尾相比上面有 /xxx)
? ? 那么實際訪問的地址是?http: // ip : port ?+ path2 +??param
規(guī)則二的作用:
如果我們訪問其它服務(wù)器都使用規(guī)則一,那么我們將無法直觀地從 URL 確定這個服務(wù)屬于那臺機器或者哪種服務(wù)。
而如果使用規(guī)則二,我們可以通過合理的設(shè)置明確地知道,我們訪問的是服務(wù)A。
root 與 index
nginx.conf 里的默認 location 如下:
location / {
root html;
index index.html;
}
意思是資源文件的根目錄在 nginx 程序所在文件夾下的 html 文件夾:
如果我們沒有寫 URI,默認的資源是 html 文件夾下的 index.html
但是你如果這樣配置:
location / {
root html;
index index.html;
}
location /index.html {
echo "index";
}
最后訪問?http://test.location.com/?瀏覽器會輸出 index:
也就是說,雖然前面設(shè)置了 index ,但是如果它對應(yīng)的地址有被 location 匹配到的話,會執(zhí)行 location 匹配后的結(jié)果。
但是,必須還是得有 index 對應(yīng)的文件。
為了驗證上面這一點,我把 html 目錄下的 index.html 改成 index,再 reload 并訪問:
root 與 alias
還是先看示例比較直觀:
server {
listen 80;
server_name test.local.com;
#文件地址:/etc/nginx/html/static/a.html
#訪問路徑:http://test.local.com/static/a.html
location /static {
#path1:/static path2:/a.html
root /etc/nginx/html/;
#文件地址:root + path1 + path2
}
#訪問路徑:http://test.local.com/target/a.html
location /target {
#path1:/target path2:/a.html
alias /etc/nginx/html/static/;
#文件地址:alias + path2
}
}
解讀:
文件地址一樣,訪問路徑可以不同。
使用 root 聲明的是根目錄,經(jīng)過 location 匹配后,直接去根目錄下找訪問路徑中 sever_name 后面的文件地址。
使用 alias 聲明的是別名目錄,經(jīng)過 location 匹配后,去別名目錄下找訪問路徑中 sever_name 后面除去 location 匹配的地址的剩余地址。這就好像給 location 匹配的地址取了個別名一樣。
規(guī)則:
1.?訪問路徑格式為 sever_name + path1 + path2?
2.??location 后面的路徑就是 path1,對照訪問路徑格式,path1 后面的就是 path2
3. 如果使用 root ,文件地址為:
? ? root + path1 +path2
4. 如果使用 alias ,文件地址為:
? ?alias + path2
location 執(zhí)行過程
結(jié)合以上內(nèi)容,就很容易理解 location 執(zhí)行過程了,如下圖
rewrite 重定向
location 里還可以配置 rewrite 實現(xiàn)重定向:
rewrite regex replacement [flag];
其中:
regex:正則表達式
replacement :替換值flag:后續(xù)處理標識,可以為 break/last/permanent/redirect
重點在于 flag :
1. flag=break
發(fā)生 nginx 內(nèi)部重定向,path值被更新,rewrite層面的命令會中斷。原控制流程邏輯不變往下走
2. flag=last
發(fā)生nginx內(nèi)部重定向,path值被更新,rewrite層面的命令會中斷??刂屏鞒趟⑿?,重新進行整個location層的邏輯流程
3. flag= permanent/redirect
發(fā)生頁面重定向(301永久重定向/302臨時重定向),nginx流程結(jié)束,返回http響應(yīng)到瀏覽器,頁面url更新
4.flag為空
發(fā)生nginx內(nèi)部重定向,path值被更新,rewrite層面的命令繼續(xù)。最后一個rewrite完畢,刷新控制流程,重新進行l(wèi)ocation重匹配
示例:
server {
listen 80;
server_name test.location.com;
location /a.html {
echo 'I am a.html';
}
location /b.html {
echo 'I am b.html';
}
#此路徑請求:http://test.location.com/aa.html
location /aa.html {##內(nèi)部重定向
rewrite ^/ /a.html break;##不會執(zhí)行下面的rewrite
rewrite ^/ /b.html break;
root /etc/nginx/html/;
}
#此路徑請求:http://test.location.com/ab.html
location /ab.html {##內(nèi)部重定向
rewrite ^/ /a.html last;##不會執(zhí)行下面的rewrite,但重新location匹配
rewrite ^/ /b.html last;
rewrite ^/ /c.html;
root /etc/nginx/html/;
}
#此路徑請求:http://test.location.com/ba
location /ba {
rewrite ^/ /b.html permanent;##301永久重定向
root /etc/nginx/html/;
}
#此路徑請求:http://test.location.com/bb
location /bb {
rewrite ^/ /b.html redirect;##302臨時重定向
set $aa 12;
root /etc/nginx/html/;
}
#此路徑請求:http://test.location.com/cc.html
location /cc.html {
rewrite ^/ /c.html;##指令不停,繼續(xù)往下
rewrite ^/ /b.html;
rewrite ^/ /a.html;##最后一條,生效的是這條
root /etc/nginx/html/;
}
}
結(jié)果:
訪問?http://test.location.com/aa.html,直接尋找 root 目錄下的 a.html,我沒有寫這個 html,所以是 404
訪問 http://test.location.com/ab.html,因為重新 location 匹配了,所以匹配到了上面的 /a.html,輸出了一句話
訪問?http://test.location.com/ba,永久重定向,可以看到地址欄的網(wǎng)址都變了,是外部重定向
訪問?http://test.location.com/bb,臨時重定向
訪問?http://test.location.com/cc.html,不是 404,也經(jīng)過了重新 location 匹配
Nginx 處理請求的11個階段
Nginx 處理請求的全過程一共劃分為 11 個階段(如圖),按階段由上到下依次執(zhí)行 (上一階段的所有指令執(zhí)行完畢,才進入下一階段)
各階段的含義如下:
- post_read: 接收到完整的 http 頭部后處理的階段,在uri重寫之前。一般跳過
- server_rewrite: location匹配前,修改uri的階段,用于重定向,location塊外的重寫指令(多次執(zhí)行)
- find_config: uri 尋找匹配的location塊配置項(多次執(zhí)行)
- rewrite:找到location塊后再修改uri,location級別的uri重寫階段(多次執(zhí)行)
- post_rewrite:防死循環(huán),跳轉(zhuǎn)到對應(yīng)階段
- preaccess: 權(quán)限預(yù)處理
- access:判斷是否允許這個請求進入
- post_access: 向用戶發(fā)送拒絕服務(wù)的錯誤碼,用來響應(yīng)上一階段的拒絕
- try_files: 訪問靜態(tài)文件資源
- content : 內(nèi)容生成階段,該階段產(chǎn)生響應(yīng),并發(fā)送到客戶端
- log:記錄訪問日志
原文鏈接:https://blog.csdn.net/weixin_44367006/article/details/101715799
相關(guān)推薦
- 2022-11-04 go語言中布隆過濾器低空間成本判斷元素是否存在方式_Golang
- 2022-10-24 centos編譯安裝mariadb的詳細過程_mariadb
- 2022-11-17 Android顯式Intent與隱式Intent的使用詳解_Android
- 2022-03-24 android?Launcher?AppWidget添加步驟介紹_Android
- 2022-10-12 python繪制發(fā)散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖_python
- 2023-05-14 Python中數(shù)字(Number)數(shù)據(jù)類型常用操作_python
- 2022-04-01 C#實現(xiàn)給PDF文檔設(shè)置過期時間_C#教程
- 2022-09-18 C++如何判斷一個數(shù)是不是素數(shù)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(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同步修改后的遠程分支