nginx的location匹配逻辑(翻译自官网)

A regular expression is preceded with the tilde (~) for case-sensitive matching, or the tilde-asterisk (~*) for case-insensitive matching. The following example matches URIs that include the string .html or .htmin any position.

(以~开头的正则表达式是对大小写敏感的),~*是不关心大小写,下面的URIs匹配的html和html

location ~ \.html? {
    ...
}

To find the location that best matches a URI, NGINX Plus first compares the URI to the locations with a prefix string. It then searches the locations with a regular expression.

为了匹配URI,NGINX首先匹配字符串前缀(prefix string),再匹配正则表达式

Higher priority is given to regular expressions, unless the ^~ modifier is used. Among the prefix strings NGINX Plus selects the most specific one (that is, the longest and most complete string). The exact logic for selecting a location to process a request is given below:

如果不使用^~修饰符,正则表达式会有更高的优先级。在字符串前缀中,nginx会找到最匹配的那个(最长,最完整的).下面给出了完整的匹配逻辑。

  1. Test the URI against all prefix strings. (URI匹配所有的字符串前缀)
  2. The = (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.(=修饰符规定了一个精确的匹配,如果该匹配成功,到此结束)
  3. If the ^~ (caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.(如果^~修饰符预先加在最长匹配的字符串前缀那里,那么其他的正则表达式不会被使用)
  4. Store the longest matching prefix string.(存储匹配最长的字符串前缀)
  5. Test the URI against regular expressions.(URI和正则表达式匹配)
  6. Break on the first matching regular expression and use the corresponding location.(第一个匹配的正则表达式那里终端,并且使用对应的那个location)
  7. If no regular expression matches, use the location corresponding to the stored prefix string.(如果没有正则表达式匹配,那么使用存储字符串前缀的那个location)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值