网站首页 > 技术教程 正文
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-10-11 Docker四种网络模式(Bridge,Host,Container,...
- 2024-10-11 HTTP 头字段 Origin、Host 和 Referer 有什么区别?
- 2024-10-11 每日漏洞 | Host头攻击(http头hostname攻击漏洞)
- 2024-10-11 Docker容器实战之数据持久化+网络模式+资源限制
- 2024-09-19 Nginx Proxy Manager - Docker 建站最佳伴侣
- 2024-09-19 高性能负载均衡:nginx搭建tomcat集群
- 2024-09-19 Nginx路由匹配规则location的小总结
- 2024-09-19 使用 nginx 反向代理多个 docker 容器
- 2024-09-19 Nginx配置虚拟主机的三种方式(nginx虚拟主机配置文件)
- 2024-09-19 Nginx开启gzip压缩演示(nginx怎么打开)
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)