首页 » PHP教程 » _locationphp技巧_nginx的location指令实战示例匹配顺序匹配冲突

_locationphp技巧_nginx的location指令实战示例匹配顺序匹配冲突

访客 2024-11-20 0

扫一扫用手机浏览

文章目录 [+]

下边我们去掉精确匹配:

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"; }}访问测试

如下可见,还剩 字串匹配、正则匹配、通用匹配,结果匹配到了 字串匹配。

_locationphp技巧_nginx的location指令实战示例匹配顺序匹配冲突

[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 示例(正则表达式间前边的为准)

上例中我们看到:~在前边,因此先匹配了 ~。
如果我们把~和~换个位置

_locationphp技巧_nginx的location指令实战示例匹配顺序匹配冲突
(图片来自网络侵删)
配置文件

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

相关文章

今日头条算法岗位面试核心方法与必备知识

大数据、人工智能等技术在各行各业得到了广泛应用。今日头条作为中国领先的资讯平台,其算法技术更是备受关注。今日头条算法岗位面试成为了...

PHP教程 2025-01-31 阅读1 评论0

今日头条算法推送如何打造个化阅读体验

在互联网时代,信息爆炸成为常态,用户获取信息的渠道越来越多,而时间却愈发有限。如何让用户在海量信息中快速找到感兴趣的内容,成为了各...

PHP教程 2025-01-31 阅读0 评论0