nginx匹配规则 - 英文文档笔记(二)

对于nginx location 模块的匹配规则,相信大部分人在开始时都会有疑问,然后就大量百度,但提醒一下,如果英文水平还过得去,极力推荐直接阅读官方文档,你会发现一下子豁然开朗。

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

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

  • 首先 location 的修饰符主要有四个,分别是:= 、^~ 、~ 、~* 。
  • 可以把 location 的匹配规则归为两类,正则匹配(regular expression):以 ~ 、~* 开头;前缀匹配(prefix string):不是正则匹配的,就叫前缀匹配。
  • ~ 开头:表示区分大小写的正则匹配。
  • ~* 开头:表示不区分大小写的正则匹配。
  • 匹配过程:首先,是搜索前缀匹配(prefix string),并且暂存最长的匹配;然后继续按配置的顺序进行正则匹配(regular expression),如果正则全部无法匹配,就使用暂存的那个前缀匹配,一旦有正则匹配成功,就结束整个匹配过程并使用此正则匹配,所以正则匹配在配置文件中的顺序是要注意的。
  • 另外:如果暂存的最长前缀匹配是以 ^~ 开头,那就不再进行正则匹配;以 = 开头的前缀匹配表示精确匹配,优先级最高,一旦成功匹配,整个匹配过程结束。

In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.

  • 在 0.7.1 至 0.8.41 版本中,一旦前缀匹配(prefix string)能成功匹配,整个搜索过程就结束,不再进行正则匹配。

Let’s illustrate the above by an example:

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

  • 官方的规则匹配小例子,并不难。
  • 以 @ 开头是标识一个 location,并不是用来进行搜索匹配的,而是用来进行重定向的。

看完这些官方文档说明,应该能清晰理解nginx的匹配过程了。

所以高手的养成是从看官方文档开始的。。。^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值