Nginx location命令匹配规则

Nginx location命令匹配规则

准备知识

location 匹配命令

  • ~ #波浪线,表示执行一个正则匹配,区分大小写
  • ~* #波浪线+星号,表示执行一个正则匹配,不区分大小写
  • ^~ #^~,表示普通字符匹配,若符合当前匹配,则只匹配当前项,不继续匹配别的选项
  • = #普通字符精准匹配
  • @ #“@”定义一个命名location,使用在内部定向时

location 匹配优先级(与location在配置文件中的位置无关)

  1. = #精准匹配会第一个被处理,若有匹配项,则不再继续匹配其他location
  2. ^~ #有匹配项,则不再继续匹配其他location
  3. ~和~* #找到相应匹配则停止匹配其他location,正则匹配按照配置文件中的位置先后匹配

总的来说,就是先匹配精准匹配(=)及普通字符精准匹配(^~),精准匹配(=)及普通字符精准匹配(^~)都是匹配到最长符合要求的请求。正则表达式的匹配(~ and ~*)按照其在配置文件中的位置来,如果正则表达式出现第一个匹配,则不再继续其他正则的匹配,如果没有匹配项,则使用早先记录的,前缀匹配(也就是普通字符匹配)符合的结果。具体规则可以参看Wiki nginx官方文档的解释:

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

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 possible compression 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 Mac OS X 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.

例子

location  = / {
  # 只匹配"/".
  [ configuration A ] 
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
  [ configuration C ] 
}
location ~* .(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求. 
  # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
  [ configuration D ] 
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NGINXlocation指令是配置文件中的关键部分,用于定义服务器或虚拟主机上URL路径(URI)与处理请求的特定块之间的映射关系。location规则是基于URI路径、正则表达式、主机名、端口号等条件来匹配请求的。以下是location规则的一些详细规则: 1. **基本语法**: ``` location /path/ { // 处理规则 } ``` 这里 `/path/` 是路径,所有匹配该路径的请求将被这里的配置处理。 2. **路径匹配**: - **精确匹配**: `/file.html` 会精确匹配到指定的文件。 - **通配符**: `*`, `**` 用于匹配任意路径和目录,如 `/images/*` 匹配 `/images/file.jpg`。 3. **正则表达式匹配**: 使用 `~` 或 `~*` 符号可以使用正则表达式进行路径匹配。例如 `location ~ \.php$ {}` 可以匹配所有以 `.php` 结尾的路径。 4. **主机名和端口匹配**: - `host`: `location = /path/ { ... }` 只对特定主机生效。 - `$host`: `location = $host/path/ { ... }` 对当前请求的主机生效。 - `server_name`: 配置虚拟主机时,可以指定多个域名或通配符。 5. **路径后缀匹配**: `try_files` 命令用于尝试查找文件的位置,如果第一个位置不存在,则尝试下一个。例如: ``` try_files $uri $uri/ =404; ``` 6. **条件指令**: - `if` 语句可以用来根据条件动态改变location的行为。 - `break` 和 `return` 关键字用于跳出当前location或整个location块。 7. **内置变量**: 可以使用 `$request_uri`, `$document_root`, `$scheme` 等内置变量进行匹配或处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值