Nginx location匹配规则

 

1、语法

location [=|~|~*|^~|@] /uri/ {   ... } 

2、说明

从上面的语法出发,可以了解到 location 可以区分为三个部分,接下来一个一个的研究一下。

1) [=|~|~*|^~|@]

  • = : 表示精确匹配后面的url
  • ~ : 表示正则匹配,但是区分大小写
  • ~* : 正则匹配,不区分大小写
  • ^~ : 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
  • @ : "@" 定义一个命名的 location,使用在内部定向时,例如 error_page

上面定义了几个不同的符号,表示不同的匹配规则,那么先后顺序呢?

  1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索;
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^~ 前缀,搜索停止;
  3. 正则表达式,在配置文件中定义的顺序;
  4. 如果第 3 条规则产生匹配的话,结果被使用。否则,使用第 2 条规则的结果。

测试示例1:

location = /world {     return 600; }  location = /hello {     return 600; }  location ~ /hellowo {     return 602; }  location ^~ /hello {     return 601; }
- 请求 localhost/world 返回600 - 请求 localhost/world2 localhost/test/world 返回其他 - 请求 localhost/hello  返回600 - 请求 localhost/hello/123 返回601 - 请求 localhost/hellow 返回601 - 请求 localhost/hellowo 返回601 - 请求 localhost/test/hellowo  返回602 - 请求 localhost/test/hello 返回其他

因此可以知道:

  • = 是精确完整匹配,且优先级最高;
  • 正则匹配时,如果 ~ 和 ^~ 同时匹配规则,则 ^~ 优先;
  • ^~ 这个规则不会匹配请求 url 中后面的路径,如上面的 /test/hello 没有匹配上
  • ^~ 不支持正则,和 = 相比,范围更广,hellowo 是可以被 ^~ 匹配,但是 = 不会匹配;
  • ~ 路径中只要包含就可以匹配,如上面的 /test/hellowo 返回了 602

测试示例2:

location ~ /hello {   return 602; }  location ~ /helloworld {   return 601; }
- 请求 localhost/world/helloworld 返回 602 - 请求 localhost/helloworld 返回 602

调整上面的顺序

location ~ /helloworld {   return 601; }  location ~ /hello {   return 602; }
- 请求 localhost/helloworld 返回601 - 请求 localhost/world/helloworld 返回601 - 请求 localhost/helloWorld 返回602

所以同时正则匹配时

  • 放在前面的优先匹配
  • 关键字:

50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信