nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx location指令

nginx location指令(匹配顺序匹配冲突)实战示例详解

作者:开发运维玄德公

这篇文章主要介绍了nginx location指令(实战示例匹配顺序匹配冲突)详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

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指令的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文