nginx location 执行规则

网上很多教程都是错的,这里参考官方文档,整理了一下.整理文档时nginx版本为 nginx-1.18.0,后序新版本可以参考新的官方文档,一般不会大变

官方文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#location

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

Sets configuration depending on a request 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.

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 blocks can be nested, with some exceptions mentioned below.

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.

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.

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.

 

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 were not 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 match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

 

总结: 

1. URI不包括参数 /a/b.html?id=1 的URI 为/a/b.html

2.URI 是url解码后的值,如果多个连续斜杠会被合并成一个斜杠

3.Location表达式分为普通表达式和正则表达式,正则表达式以(~ ~*)开头,其它的表达式都为普通表达式

4.nginx先匹配普通表达式,再匹配正则表达式,如果正则表达式匹配到则使用正则匹配结果,如果未找到则使用普通表达式匹配结果

5.匹配普通表达式,选择最长前缀的结果作为备选,如果最长前缀以 ^~或=开头,则停止正则匹配

6.如果进行正则匹配,第一次匹配后即返回结果,不再进行其它正则表达式匹配

7.普通匹配和表达式配置前后位置无关,正则匹配按照配置文件中先后顺序进行匹配

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值