Nginx location命令匹配规则

来源:互联网 发布:好的java培训机构 编辑:程序博客网 时间:2024/06/05 20:09

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 ] }
0 0
原创粉丝点击