Nginx location匹配规则的方法示例

2019-10-17 17:14:32刘景俊

1、语法

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

2、说明

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

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

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

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

    = 前缀的指令严格匹配这个查询。如果找到,停止搜索; 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^~ 前缀,搜索停止; 正则表达式,在配置文件中定义的顺序; 如果第 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

所以同时正则匹配时

放在前面的优先匹配 注意如果不区分大小写时,使用 ~* 尽量将精确匹配的放在前面

测试示例3:

location ^~ /hello/ {
 return 601;
}

location /hello/world {
 return 602;
}

这种场景中,存在一个没有符合的路由规则,那么实际的测试是怎样呢?

- http://localhost/hello/wor 返回601
- http://localhost/hello/world 返回602
- http://localhost/hello/world23 返回602
- http://localhost/hello/world/123 返回602