学习一样东西,最直观的就是学习它的官方文档,Nginx也不例外。本文要学习的是Nginx配置文件中Location的配置规则。
官方文档链接:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
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.
官方文档上这么说:
location既可以通过前缀匹配,也可以通过正则匹配。正则匹配由“~*” (不区分大小写)或者“~”(区分大小写)开头。要匹配给定请求的location,nginx首先进行前缀匹配,其中最长匹配的那个location会被记录下来。然后nginx按照在配置文件中从上到下的顺序进行正则匹配。当找一个匹配的正则表达式后,该location就会被使用。如果没有匹配到正则表达式,之前记录的最长匹配的location便会被使用。
以上便是对官方文档的解读,是不是感觉好简单?
网友评论