您的位置:首页 > 运维架构 > Nginx

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

2016-01-22 15:52 597 查看
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
[code]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会找到最匹配的那个(最长,最完整的).下面给出了完整的匹配逻辑。

Test the URI against all prefix strings. (URI匹配所有的字符串前缀)

The
=
(equals sign) modifier defines
an exact match of the URI and a prefix string. If the exact match is found, the search stops.(=修饰符规定了一个精确的匹配,如果改匹配成功,到此结束)

If the
^~
(caret-tilde) modifier
prepends the longest matching prefix string, the regular expressions are not checked.(如果^~修饰符预先加在最长匹配的字符串前缀那里,那么正则表达式不会被检查)

Store the longest matching prefix string.(存储匹配最长的字符串前缀)

Test the URI against regular expressions.(URI和正则表达式匹配)

Break on the first matching regular expression and use the corresponding location.(第一个匹配的正则表达式那里终端,并且使用对应的那个location)

If no regular expression matches, use the location corresponding to the stored prefix string.(如果没有正则表达式匹配,那么使用存储字符串前缀的那个location)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: