Home >Backend Development >PHP Tutorial >nginx location matching rules

nginx location matching rules

WBOY
WBOYOriginal
2016-07-28 08:27:371170browse

location matching command

~ #The wavy line means performing a regular match, case-sensitive
~* #Indicates performing a regular match, case-insensitive
^~ #^~ means normal character matching, if the option matches , only matches this option and does not match other options. It is generally used to match directories
=   #Exact matching of ordinary characters
@   #"@" Defines a named location, used for internal orientation, such as error_page, try_files

The priority of location matching (regardless of the order of locations in the configuration file)
= Exact matches will be processed first. If an exact match is found, nginx stops searching for other matches.
Ordinary character matching, regular expression rules and long block rules will be prioritized for query matching, which means that if the item matches, you need to check whether there are regular expression matches and longer matches.
^~ will only match this rule, and nginx will stop searching for other matches, otherwise nginx will continue to process other location instructions.
Finally match the instructions with "~" and "~*". If a corresponding match is found, nginx stops searching for other matches; when there is no regular expression or no regular expression is matched, then the matching degree is the highest The verbatim matching directive will be used.

location priority official document

  1. Directives with the = prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix , searching stops.
  3. Regular expressions, in order of definition in the configuration file.
  4. If #3 yielded a match, that result is used. Else the match from #2 is used.
  1. =prefixed directive strict match this query. If found, stop searching.
  2. All remaining regular strings, longest match. If the match uses the ^? prefix, the search stops.
  3. Regular expression, in the order defined in the configuration file.
  4. If rule 3 produces a match, the result is used. Otherwise, the same as from rule 2 is used.

For example

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 ] 
}

The above introduces the nginx location matching rules, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Nginx configuration notesNext article:Nginx configuration notes