nginx location 指令说明

Syntax:location [ = | ~ | ~* |^~ ] uri { ... }
location @name { ... }
Default:
Context:server, location

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 possiblecompression of two or more adjacent slashes into a single slash.

匹配是同通常的URI进行,文本被编码成"%XX"格式后,就参考相关的路径组件去解决。对于"."和 "..",两个或者多个相邻的"/"有可能会压缩成单个"/" 。

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 matchingprefix is selected and remembered.Then regular expressions are checked, in the order of their appearancein 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 theconfiguration of the prefix location remembered earlier is used.

location既能用前缀字符串定义也可以用正则表达式定义。正则表达式以"~*"修饰符开头(大小写敏感的匹配),或者以"~" 修饰符开头(大小写不敏感的匹配)。找到一个location去匹配一个指定的请求,nginx首先去匹配用前缀字符串定义的location(前缀locations)。在这些前缀location之间,最长匹配前缀的将会被选择并记住。然后,检查正则表达式定义的location,匹配顺序按照正则表达式location在配置文件中出现的顺序进行匹配。正则表达式的匹配在第一个匹配上的location的地方结束,然后这个location的配置会被使用,如果没有找到匹配到的正则表达式,就使用先前在前缀location中匹配到的location。

location blocks can be nested, with some exceptions mentioned below.

location block可以被内嵌到除下面提到的指令中。

For case-insensitive operating systems such as Mac OS X and Cygwin,matching with prefix strings ignores a case (0.7.7).However, comparison is limited to one-byte locales.

对于像Mac OS X 和 Cygwin这样的大小写敏感的操作系统,匹配前缀字符串的时候要忽略大小写。然而,压缩仅限于在一个字节的地方。

Regular expressions can contain captures (0.7.40) that can later be used in other directives.


If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

如果最长匹配的前缀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.

同样,使用“=”修饰符主要可能是去定义一个精确的URI以及location。如果一个精确的匹配被找到,搜索终止。比如,如果一个“/”的请求出现的很频繁,那么定义一个“location = /”将会加速这些请求的执行。因为在第一个比较的时候就会终止搜索了。此类location不能明显的包含嵌入的location。

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 werenot checked.

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 matchconfiguration C,the “/images/1.gif” request will match configuration D, andthe “/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.

If a location is defined by a prefix string that ends with the slash character,and requests are processed by one ofproxy_pass,fastcgi_pass,uwsgi_pass,scgi_pass, ormemcached_pass,then the special processing is performed.In response to a request with URI equal to this string,but without the trailing slash,a permanent redirect with the code 301 will be returned to the requested URI with the slash appended.If this is not desired, an exact match of the URI and location could bedefined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值