网站首页 > 精选教程 正文
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
猜你喜欢
- 2024-09-27 Nginx的Location到底是怎么匹配的?
- 2024-09-27 Nginx从入门到放弃02-Nginx基本命令和新建WEB站点
- 2024-09-27 Nginx 实践:location 路径匹配 nginx配置文件中的location
- 2024-09-27 Nginx配置文件中location的优先级答疑
- 2024-09-27 nginx配置之location nginx 配置location
- 2024-09-27 nginx中什么是命名位置(named locations)?
- 2024-09-27 nginx location在配置中的优先级 nginxlocation规则
- 2024-09-27 理解Nginx的location nginx location /api
- 2024-09-27 一分钟搞清楚:Nginx之Location优先级
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)