Introduction to location matching rules in Nginx

  • 2020-05-09 19:57:51
  • OfStack

location matches the command

The ~           # squiggly line means to perform a regular match, case sensitive
~*       # means to perform a regular match, case insensitive
^~       #^~ indicates normal character matching. If this option matches, match only this option, do not match other options
=           # for exact matching of normal characters
        #"@" defines a named location when used for internal orientation, such as error_page, try_files

The priority of location matches (regardless of the order of location in the configuration file)
= the exact match will be processed in the first. If an exact match is found, nginx stops searching for other matches.
Normal character matching, regular expression rules and long block rules will be matched prior to the query, which means that if the item is matched, it needs to see if there is a regular expression match and a longer match.
^~ only matches this rule, nginx stops searching for other matches, otherwise nginx continues processing other location instructions.
Finally, match the instruction with "~" and "~*". If the corresponding match is found, nginx will stop searching for other matches. The verbatim matching instruction with the highest degree of matching is used when no regular expression is present or no regular expression is matched.

location priority official documentation

The directive       = prefix matches this query exactly. If found, stop searching.
      all remaining regular strings, longest match. If this match USES ^? Prefix, search stops.
      regular expressions, the order defined in the configuration file.
      if the third rule produces a match, the result is used. Otherwise, as from rule 2 is used.

For example,

location  = / {
  # Matches only "/".
  [ configuration A ]
}
location  / {
  # Matches any request, because all requests are "/" start
  # But longer character matching or regular expression matching will take precedence
  [ configuration B ]
}
location ^~ /images/ {
  # Match any with /images/ Start the request and stop the match other location
  [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # Match with gif, jpg, or jpeg Closing request .
  # But all /images/ Directory requests will be made by [Configuration C] To deal with .  
  [ configuration D ]
}
 
location  = / {
  # Matches only "/".
  [ configuration A ]
}
location  / {
  # Matches any request, because all requests are "/" start
  # But longer character matching or regular expression matching will take precedence
  [ configuration B ]
}
location ^~ /images/ {
  # Match any with /images/ Start the request and stop the match other location
  [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # Match with gif, jpg, or jpeg Closing request .
  # But all /images/ Directory requests will be made by [Configuration C] To deal with .  
  [ configuration D ]
}

Request URI example:

    / ->  Conform to the configuration A
    /documents/document.html -> Conform to the configuration B
    /images/1.gif -> Conform to the configuration C
    /documents/1.jpg -> Conform to the configuration D @location example
error_page 404 = @fetch; location @fetch(
proxy_pass http://fetch;
)


Related articles: