公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

目录
  • 1. 对url的匹配
    • 1.1 默认匹配
    • 1.2 精确匹配( = )
    • 1.3 正则,区分大小写 ( ~ )
    • 1.4 正则表达式,不区分大小写 ( ~* )
  • 2. 匹配顺序
    • 2.1 示例(精确匹配最高)
    • 2.2 示例(字串匹配次之)
    • 2.3 示例(正则匹间配高于通用匹配)
    • 2.4 示例(正则表达式间前边的为准)
    • 2.5 示例(通用匹配兜底)
  • 3. 匹配间的冲突
    • 3.1 通用匹配 VS 字串匹配

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

分享到:
标签:匹配 实战 指令 示例 详解
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定