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

 更新时间:2023年06月21日 11:37:54   作者:开发运维玄德公  
这篇文章主要介绍了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指令的资料请关注脚本之家其它相关文章!

相关文章

  • 详解Nginx中的geo模块与利用其配置负载均衡的示例

    详解Nginx中的geo模块与利用其配置负载均衡的示例

    这篇文章主要介绍了详解Nginx中的geo模块与利用其配置负载均衡的示例,文中对模块的geo指令使用有比较详细的介绍,需要的朋友可以参考下
    2016-01-01
  • 一文详解nginx中的root与alias

    一文详解nginx中的root与alias

    Nginx是一款流行的高性能Web服务器和反向代理服务器,这篇文章主要给大家介绍了关于如何通过一文详解nginx中的root与alias的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • nginx反向代理配置去除前缀

    nginx反向代理配置去除前缀

    这篇文章主要介绍了nginx反向代理配置去除前缀的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Nginx响应头Vary介绍与应用小结

    Nginx响应头Vary介绍与应用小结

    响应头部字段在控制缓存行为、优化性能等方面起着重要作用,Vary头部字段是其中一个关键字段,它用于指示缓存代理在何种条件下缓存响应,下面就来详细的介绍一下,感兴趣的可以了解一下
    2025-09-09
  • Nginx中配置用户服务器访问认证的方法示例

    Nginx中配置用户服务器访问认证的方法示例

    这篇文章主要介绍了Nginx中配置用户服务器访问认证的方法示例,包括一个用perl脚本来实现的方法,需要的朋友可以参考下
    2016-01-01
  • Nginx启用gzip压缩的方法示例

    Nginx启用gzip压缩的方法示例

    这篇文章主要介绍了Nginx启用gzip压缩的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Nginx重定向后请求参数丢失的原因分析及解决方案

    Nginx重定向后请求参数丢失的原因分析及解决方案

    在日常开发和运维中,我们经常会遇到需要使用 Nginx 进行反向代理的场景,但在配置 proxy_pass 时,有时候可能会遇到请求参数丢失的问题,在这篇文章中,我们将会详细探讨这个问题并给出几种解决方案,需要的朋友可以参考下
    2023-11-11
  • Nginx+iptables屏蔽访问Web页面过于频繁的IP(防DDOS,恶意访问,采集器)

    Nginx+iptables屏蔽访问Web页面过于频繁的IP(防DDOS,恶意访问,采集器)

    通过分析nginx的日志来过滤出访问过于频繁的IP地址,然后添加到nginx的blockip.conf,并重启nginx.
    2010-11-11
  • Nginx的location的常见规则优先级问题

    Nginx的location的常见规则优先级问题

    Nginx是反向代理和负载均衡的首选工具,nginx的location配置有许多细节内容在网上不容易找到资料,或者解释不清。本文对Nginx location规则优先级介绍,需要的朋友参考下吧
    2021-08-08
  • 一文详解Nginx的强缓存和协商缓存

    一文详解Nginx的强缓存和协商缓存

    这篇文章主要为大家详细介绍了Nginx中强缓存和协商缓存的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-03-03

最新评论