nginx location優先順序

2021-08-21 01:45:27 字數 1757 閱讀 6286

目錄

1 nginx location語法規則

2 規則優先順序

3 規則示例

4 優先順序演示示例

語法規則:location [=|~|~*|^~] /uri/  

nginx的location匹配的變數是$uri

符號說明=

表示精確匹配

^~表示uri以指定字元或字串開頭

~*表示不區分大小寫的正則匹配

~表示區分大小寫的正則匹配

/通用匹配,任何請求都會匹配到

= 高於 ^~ 高於 ~* 等於 ~ 高於 /
location = "/12.jpg" 

如:bfd-wyl.cn/12.jpg 匹配

bfd-wyl.cn/abc/12.jpg 不匹配

location ^~ "/abc/"

如:bfd-wyl.cn/abc/123.html 匹配

bfd-wyl.cn/a/abc/123.jpg 不匹配

location ~ "png"

如:bfd-wyl.cn/aaa/bbb/ccc/123.png 匹配

bfd-wyl.cn/aaa/png/123.html 匹配

location ~* "png"

如:bfd-wyl.cn/aaa/bbb/ccc/123.png 匹配

bfd-wyl.cn/aaa/png/123.html 匹配

location /admin/

如:bfd-wyl.cn/admin/aaa/1.php 匹配

bfd-wyl.cn/123/admin/1.php 不匹配

1.nginx的配置內容如下:

[root@wyl01 conf.d]# cat default.conf

server

location ~ /wyl/index.html

location ~* /wyl/index.html

location ^~ /wyl

location = /wyl/index.html

}

示例1:nginx的配置檔案保持上述內容不變

#示例一 我們看到"="號優先順序最高,匹配到了最下面的location了。

[root@wyl01 conf.d]# curl -w "%" 106.12.74.123/wyl/index.html;echo ''

605

示例2:nginx的配置內容我們去掉最後乙個=匹配的location,再次驗證

[root@wyl01 conf.d]# curl  -w "%" 106.12.74.123/wyl/index.html;echo ''

604

示例3:nginx的配置內容我們去掉=和^~匹配的location,再次驗證

[root@wyl01 conf.d]# curl  -w "%" 106.12.74.123/wyl/index.html;echo ''

603

示例3:nginx的配置內容我們保留前兩個localtion,再次驗證

[root@wyl01 conf.d]# curl  -w "%" 106.12.74.123/wyl/index.html;echo ''

602

nginx location優先順序

目錄網上查了下location的優先順序規則,但是很多資料都說的模稜兩可,自己動手實地配置了下,下面總結如下。1 精確匹配 location test2 字首匹配 location testlocation test3 正則匹配 location test location test 1 多個字首匹...

擴充套件 Nginx location 優先順序

nginx location 優先順序 高於 高於 等於 高於 server location abc 測試命令 curl x127.0.0.1 80 www.aminglinux.com abc 1.png 結果是 server location abc 測試命令 curl x127.0.0.1 ...

Nginx location匹配規則及優先順序

location uri 模式 含義location uri 表示精確匹配,只有完全匹配上才能生效 location uri 開頭對url路徑進行字首匹配,並且在正則之前。location pattern 開頭表示區分大小寫的正則匹配 location pattern 開頭表示不區分大小寫的正則匹配...