location指令
Synyax:location [ = | ~ | ~* | ^~ | ] uri {...}
location @name {...}
Default:——
Context:server,location
根据请求的URI设置配置。(Sets configuration depending on a request URI.)
匹配是在一个正常的URI上进行的。(The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.)
一个location可以由一个前缀字符串定义也可以由一个正则表达式定义。正则表达式的指定是在uri之前指定~*
修饰符(大小写不敏感匹配)或者~
修饰符(大小写敏感匹配)。为了查找与指定request匹配的location,nginx首先检查使用前缀字符串定义的location(prefix locations)。其中,最长前缀匹配的location被选择同时被记住。然后正则表达式被检查,按照他们在配置文件中出现的顺序。正则表达式的搜索在找到第一个正则表达式时停止。如果没有匹配的正则表达式,那么最先记住的prefix 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 blocks can be nested, with some exceptions mentioned below.)
对一个大小写不敏感的操作系统如MacOS和Cygwin,前缀字符串匹配忽略大小写(0.7.7)。然而,比较是限制在一个字节域的。(For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.)
正则表达式可以包含后面其它命令可能使用到的captures。(Regular expressions can contain captures (0.7.40) that can later be used in other directives.)
如果最长匹配的prefix location有^~
修饰符,那么正则表达式不会被检查。(If the longest matching prefix location has the ^~
modifier then regular expressions are not checked.).
同时,使用=
修饰符来定义一个URI和location之间的精确匹配。如果一个精确匹配被发现,那么搜索终止。例如,如果一个"/"请求频繁发生,定义“location = /"可以加速这些请求的处理,因为搜索在第一次匹配时就终止了。这样的location不能显示的嵌套location。(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.)
从版本0.7.1到0.8.41,如果一个请求匹配了一个没有
=
和^~
修饰符的prefix location,搜索会终止同时正则表达式不会被检查。
让我们用例子来说明:
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
"/"请求会匹配configuration A,“/index.html”请求会匹配configuration B,"/documents/document.html"请求会匹配configuration C,"/images/1.gif"请求会匹配configuration D,"/documnes/1.jpg"请求会匹配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.)
"@"前缀定义了一个命名location。这样的location不是用于常规的请求处理,而是用于请求重定向。它们不能嵌套,也不能包含嵌套的locations。(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由以slash字符结束的前缀字符串定义的并且请求由proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass或 memcached_pass其中的一个处理,那么将执行特殊的处理。对符合前述字符串的URI并且不包含反斜杠,一个固定的301重定向将被返回请求的URI并在该URI后添加slash。如果这满足预期,一个精确的URI和location之间的定位可以如下定义:
location /user/ {
proxy_pass http://user.example.com;
}
location = /user {
proxy_pass http://login.example.com;
}