location /crow/ {
return 501 "通用匹配\n";
}
location = /crow/ {
return 501 "精确匹配\n";
}
location ~ /crow/.*\.md {
return 501 "正则表达式,区分大小写\n";
}
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
=
)^~
)~
、~*
)
)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 "正则表达式,区分大小写\n";
}
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
精确匹配
可见精确匹配被匹配到。
下边我们去掉精确匹配:
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 "正则表达式,区分大小写\n";
}
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
如下可见,还剩
字串匹配
、正则匹配
、通用匹配
,结果匹配到了字串匹配
。
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
字串匹配
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 "正则表达式,区分大小写\n";
}
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
#location ^~ /crow/test.md {
# return 501 "字串匹配\n";
#}
}
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正则表达式,区分大小写
上例中我们看到:
~
在前边,因此先匹配了~
。如果我们把~
和~*
换个位置
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 "正则表达式,不区分大小写\n";
}
location ~ /crow/.*\.md {
return 501 "正则表达式,区分大小写\n";
}
}
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正则表达式,不区分大小写
我们还是将所有匹配都写上
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 "正则表达式,区分大小写\n";
}
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
location ^~ /crow/test.md {
return 501 "字串匹配\n";
}
}
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.txt
通用匹配
通用匹配
和字串匹配
相同时,启动报错
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
3.1 很孤独,我又暂时没有发现 3.2 ,留个位置,欢迎大家补充。