nginx location configuration rules

Nginx location matching is performed according to certain rule priorities, just like operators have priorities, so they are not matched according to the order in which the configuration is written. The specific priorities are as follows:

First priority: The equal sign type (=) has the highest priority. Once a match is found, no further matches are looked for.
Second priority: ^~ type expression. Once a match is found, no further matches are looked for.
Third priority: The regular expression type (~ ~*) has the second priority. If there are multiple location regular expressions that can match, the one with the longest regular expression is used.
Fourth priority: regular string matching type. Match by prefix.

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

location expression type:

~ means to perform a regular match, case-sensitive
~* means to perform a regular match, not case-sensitive
^~ means to match ordinary characters. Use prefix matching. If the match is successful, no other locations will be matched.
= Performs an exact match of ordinary characters. That is, an exact match.
@ "@" defines a named location, used for internal orientation, such as error_page, try_files

location uri regular expression

  1. . : matches any character except newline
  2. ? : Repeat 0 or 1 times
  3. + : Repeat 1 or more times
  4. * : Repeat 0 or more times
  5. \d : match numbers
  6. ^ : matches the beginning of the string
  7. $ : matches the end of the string
  8. {n} : repeat n times
  9. {n,} : Repeat n or more times
  10. [c] : match a single character c
  11. [a-z] : Match any one of az lowercase letters
  12. (a|b|c) : The attribute line means to match any one of the cases, each case is separated by a vertical bar, generally enclosed in parentheses, and matches a character string matching a character, b character or c character
  13. \ backslash: used to escape special characters
  14. The content matched between parentheses () can $1be quoted later by passing, $2which means the content in the second () in front. What is confusing in the regex is the escape of special characters.

Priority search questions:

Different types of location mapping determine whether to continue searching downwards, equal sign type, ^~ type: once matched, the search will stop, and no other location will be matched. Regular expression type (~ ~*), regular string matching type /xxx/ : After a match is found, other locations will continue to be searched until the one with the highest priority is found, or the search is stopped when the first case is found .

Guess you like

Origin blog.csdn.net/sirria1/article/details/125060117