nginx?location指令(匹配順序匹配沖突)實(shí)戰(zhàn)示例詳解
1. 對(duì)url的匹配
1.1 默認(rèn)匹配
- 語法示例
location /crow/ {
return 501 "通用匹配\n";
}
1.2 精確匹配( = )
- 語法示例
location = /crow/ {
return 501 "精確匹配\n";
}
1.3 正則,區(qū)分大小寫 ( ~ )
- 語法示例
location ~ /crow/.*\.md {
return 501 "正則表達(dá)式,區(qū)分大小寫\n";
}
1.4 正則表達(dá)式,不區(qū)分大小寫 ( ~* )
- 語法示例
location ~* /crow/.*\.md {
return 501 "正則表達(dá)式,不區(qū)分大小寫\n";
}
2. 匹配順序
- 精確匹配(
=) - 字串匹配(
^~) - 正則匹配(
~、~*) - 默認(rèn)匹配()
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 "正則表達(dá)式,區(qū)分大小寫\n";
}
location ~* /crow/.*\.md {
return 501 "正則表達(dá)式,不區(qū)分大小寫\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
- 訪問測(cè)試
[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 "正則表達(dá)式,區(qū)分大小寫\n";
}
location ~* /crow/.*\.md {
return 501 "正則表達(dá)式,不區(qū)分大小寫\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
- 訪問測(cè)試
如下可見,還剩 字串匹配、正則匹配、通用匹配,結(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 "正則表達(dá)式,區(qū)分大小寫\n";
}
location ~* /crow/.*\.md {
return 501 "正則表達(dá)式,不區(qū)分大小寫\n";
}
#location ^~ /crow/test.md {
# return 501 "字串匹配\n";
#}
}
- 訪問測(cè)試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 正則表達(dá)式,區(qū)分大小寫
2.4 示例(正則表達(dá)式間前邊的為準(zhǔn))
上例中我們看到:~在前邊,因此先匹配了 ~。如果我們把~和~*換個(gè)位置
- 配置文件
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 "正則表達(dá)式,不區(qū)分大小寫\n";
}
location ~ /crow/.*\.md {
return 501 "正則表達(dá)式,區(qū)分大小寫\n";
}
}
- 訪問測(cè)試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 正則表達(dá)式,不區(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 "正則表達(dá)式,區(qū)分大小寫\n";
}
location ~* /crow/.*\.md {
return 501 "正則表達(dá)式,不區(qū)分大小寫\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
- 訪問測(cè)試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.txt 通用匹配
3. 匹配間的沖突
3.1 通用匹配 VS 字串匹配
通用匹配和字串匹配相同時(shí),啟動(dòng)報(bào)錯(cuò)
- 配置文件
location /crow/test.md {
return 501 "通用匹配\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
- 啟動(dòng)報(bào)錯(cuò)如下:
nginx-crow-test | nginx: [emerg] duplicate location "/crow/test.md" in /etc/nginx/conf.d/default.conf:45
以上就是nginx location指令(實(shí)戰(zhàn)示例匹配順序匹配沖突)使用詳解的詳細(xì)內(nèi)容,更多關(guān)于nginx location指令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
nginx反向代理導(dǎo)致session失效的問題解決
這篇文章主要介紹了nginx反向代理導(dǎo)致session失效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Debian下搭建Nginx和Tomcat服務(wù)器實(shí)現(xiàn)負(fù)載均衡的方案
這篇文章主要介紹了Debian下搭建Nginx和Tomcat服務(wù)器實(shí)現(xiàn)負(fù)載均衡的方案,其主要思想依然是動(dòng)靜分離并且以Nginx來進(jìn)行反向代理這樣的路子,需要的朋友可以參考下2015-12-12
Nginx服務(wù)器中配置GeoIP模塊來攔截指定國(guó)家IP
Nginx中自帶GeoIP模塊可以屏蔽指定IP的請(qǐng)求,只不過默認(rèn)沒有被編譯,打開以后我們只要再下載一個(gè)IP規(guī)則就行,Nginx服務(wù)器中配置GeoIP模塊來攔截指定國(guó)家IP2016-06-06
Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法
本文主要介紹了Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法,報(bào)錯(cuò)413是因?yàn)镹ginx對(duì)上傳大小做了限制,所以我們需要配置文件,下面就來解決這個(gè)問題,感興趣的可以了解一下2024-03-03
如何配置Nginx每個(gè)進(jìn)程最多打開的文件數(shù)量
這篇文章主要介紹了配置Nginx每個(gè)進(jìn)程最多打開的文件數(shù)量,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
nginx:413 Request Entity Too Large的處理辦法--修改 PHP上傳文件大小
在用 phpMyAdmin 進(jìn)行 sql 數(shù)據(jù)庫導(dǎo)入的時(shí)候,經(jīng)常需要上傳比較大的 sql 數(shù)據(jù)文件,而這時(shí)會(huì)常碰見 nginx報(bào)錯(cuò):413 Request Entity Too Large。解決此問題,根據(jù)上傳數(shù)據(jù)文件的大小進(jìn)行修改處理2014-06-06
解決Nginx配置靜態(tài)資源文件404 Not Found問題
在使用Nginx作為靜態(tài)資源服務(wù)器時(shí),如果配置了根目錄root導(dǎo)致404錯(cuò)誤,而使用前綴URL配置alias則需要正確處理目錄路徑,使用alias時(shí)要確保目錄名后加‘/’,并且在需要時(shí)使用root和alias配置,本文介紹Nginx配置靜態(tài)資源文件404 Not Found問題解決方法,感興趣的朋友一起看看吧2025-03-03

