nginx?location指令(匹配順序匹配沖突)實戰(zhàn)示例詳解
1. 對url的匹配
1.1 默認匹配
- 語法示例
location /crow/ { return 501 "通用匹配\n"; }
1.2 精確匹配( = )
- 語法示例
location = /crow/ { return 501 "精確匹配\n"; }
1.3 正則,區(qū)分大小寫 ( ~ )
- 語法示例
location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; }
1.4 正則表達式,不區(qū)分大小寫 ( ~* )
- 語法示例
location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; }
2. 匹配順序
- 精確匹配(
=
) - 字串匹配(
^~
) - 正則匹配(
~
、~*
) - 默認匹配()
2.1 示例(精確匹配最高)
- 配置文件內(nèi)容:
server { listen 1840; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } location /crow/ { return 501 "通用匹配\n"; } location = /crow/test.md { return 501 "精確匹配\n"; } location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; } location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; } location ^~ /crow/test.md { return 501 "字串匹配\n"; } }
- 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 精確匹配
可見精確匹配被匹配到。
下邊我們?nèi)サ艟_匹配:
2.2 示例(字串匹配次之)
- 配置文件內(nèi)容:
server { listen 1840; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } location /crow/ { return 501 "通用匹配\n"; } #location = /crow/test.md { # return 501 "精確匹配\n"; #} location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; } location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; } location ^~ /crow/test.md { return 501 "字串匹配\n"; } }
- 訪問測試
如下可見,還剩 字串匹配
、正則匹配
、通用匹配
,結(jié)果匹配到了 字串匹配
。
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 字串匹配
2.3 示例(正則匹間配高于通用匹配)
- 配置文件
server { listen 1840; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } location /crow/ { return 501 "通用匹配\n"; } #location = /crow/test.md { # return 501 "精確匹配\n"; #} location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; } location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; } #location ^~ /crow/test.md { # return 501 "字串匹配\n"; #} }
- 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 正則表達式,區(qū)分大小寫
2.4 示例(正則表達式間前邊的為準)
上例中我們看到:~
在前邊,因此先匹配了 ~
。如果我們把~
和~*
換個位置
- 配置文件
server { listen 1840; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } location /crow/ { return 501 "通用匹配\n"; } location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; } location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; } }
- 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 正則表達式,不區(qū)分大小寫
2.5 示例(通用匹配兜底)
配置文件
我們還是將所有匹配都寫上
server { listen 1840; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } location /crow/ { return 501 "通用匹配\n"; } location = /crow/test.md { return 501 "精確匹配\n"; } location ~ /crow/.*\.md { return 501 "正則表達式,區(qū)分大小寫\n"; } location ~* /crow/.*\.md { return 501 "正則表達式,不區(qū)分大小寫\n"; } location ^~ /crow/test.md { return 501 "字串匹配\n"; } }
- 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.txt 通用匹配
3. 匹配間的沖突
3.1 通用匹配 VS 字串匹配
通用匹配
和字串匹配
相同時,啟動報錯
- 配置文件
location /crow/test.md { return 501 "通用匹配\n"; } location ^~ /crow/test.md { return 501 "字串匹配\n"; }
- 啟動報錯如下:
nginx-crow-test | nginx: [emerg] duplicate location "/crow/test.md" in /etc/nginx/conf.d/default.conf:45
以上就是nginx location指令(實戰(zhàn)示例匹配順序匹配沖突)使用詳解的詳細內(nèi)容,更多關(guān)于nginx location指令的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Debian下搭建Nginx和Tomcat服務器實現(xiàn)負載均衡的方案
這篇文章主要介紹了Debian下搭建Nginx和Tomcat服務器實現(xiàn)負載均衡的方案,其主要思想依然是動靜分離并且以Nginx來進行反向代理這樣的路子,需要的朋友可以參考下2015-12-12nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小
在用 phpMyAdmin 進行 sql 數(shù)據(jù)庫導入的時候,經(jīng)常需要上傳比較大的 sql 數(shù)據(jù)文件,而這時會常碰見 nginx報錯:413 Request Entity Too Large。解決此問題,根據(jù)上傳數(shù)據(jù)文件的大小進行修改處理2014-06-06解決Nginx配置靜態(tài)資源文件404 Not Found問題
在使用Nginx作為靜態(tài)資源服務器時,如果配置了根目錄root導致404錯誤,而使用前綴URL配置alias則需要正確處理目錄路徑,使用alias時要確保目錄名后加‘/’,并且在需要時使用root和alias配置,本文介紹Nginx配置靜態(tài)資源文件404 Not Found問題解決方法,感興趣的朋友一起看看吧2025-03-03