nginx location指令(匹配顺序匹配冲突)实战示例详解
目录
- 1. 对url的匹配
- 1.1 默认匹配
- 1.2 精确匹配( = )
- 1.3 正则,区分大小写 ( ~ )
- 1.4 正则表达式,不区分大小写 ( ~* )
- 2. 匹配顺序
- 2.1 示例(精确匹配最高)
- 2.2 示例(字串匹配次之)
- 2.3 示例(正则匹间配高于通用匹配)
- 2.4 示例(正则表达式间前边的为准)
- 2.5 示例(通用匹配兜底)
- 3. 匹配间的冲突
- 3.1 通用匹配 VS 字串匹配
1. 对url的匹配
1.1 默认匹配
- 语法示例
location /crow/ {
return 501 "通用匹配\n";
}
1.2 精确匹配( = )
- 语法示例
location = /crow/ {
return 501 "精确匹配\n";
}
1.3 正则,区分大小写 ( ~ )
- 语法示例
location ~ /crow/.*\.md {
return 501 "正则表达式,区分大小写\n";
}
1.4 正则表达式,不区分大小写 ( ~* )
- 语法示例
location ~* /crow/.*\.md {
return 501 "正则表达式,不区分大小写\n";
}
2. 匹配顺序
- 精确匹配(
=) - 字串匹配(
^~) - 正则匹配(
~、~*) - 默认匹配()
2.1 示例(精确匹配最高)
- 配置文件内容:
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 精确匹配
可见精确匹配被匹配到。
下边我们去掉精确匹配:
2.2 示例(字串匹配次之)
- 配置文件内容:
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 字串匹配
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 "正则表达式,区分大小写\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 正则表达式,区分大小写
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 "正则表达式,不区分大小写\n";
}
location ~ /crow/.*\.md {
return 501 "正则表达式,区分大小写\n";
}
}
- 访问测试
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md 正则表达式,不区分大小写
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 "正则表达式,区分大小写\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 通用匹配
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指令(实战示例匹配顺序匹配冲突)使用详解的详细内容,更多关于nginx location指令的资料请关注其它相关文章!
您可能感兴趣的文章
- 12-20Kubernetes中使用临时容器进行故障排查的方法
- 12-20Nginx设置HTTPS的方法步骤
- 12-20二进制方式安装 Kubernetes1.18.3版本实现脚本
- 12-20Nginx工作模式及代理配置的使用细节
- 12-20ZooKeeper分布式协调服务设计核心概念及安装配置
- 12-20Kubernetes部署可视化地图的十个步骤
- 12-20关于docker清理Overlay2占用磁盘空间的问题(亲测有效)
- 12-20Docker compose配置文件写法及命令使用示例
- 12-20openwrt安装docker并启动的操作方法
- 12-20云原生Kubernetes初始化容器Init使用教程


阅读排行
推荐教程
- 12-07一文教你怎么选择Tomcat对应的JDK版本
- 12-07新版Eclipse集成Tomcat时找不到server选项的解决方法
- 12-06IIS7 应用程序池自动回收关闭的解决方案
- 12-05Windows Server 2019安装VMware
- 12-05Windows服务器默认IE浏览器无法下载文件的解决方法
- 12-05Docker安装Jenkins全过程
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法





