What order do location directives fire in?
4 Answers
From the HTTP core module docs:
- Directives with the "=" prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
- Regular expressions, in the order they are defined in the configuration file.
- If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.
Example from the documentation:
location = / {
# matches the query / only.
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
[ configuration C ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration D.
[ configuration E ]
}
If it's still confusing, here's a longer explanation.
-
14with it can help you :)github.com/detailyang/nginx-location-match-visible Aug 1, 2016 at 7:33
-
11Note that both
/
and/documents/
rules match the request/documents/index.html
, but the latter rule takes precedence since it's the longest rule. Aug 13, 2017 at 19:23
It fires in this order.
=
(exactly)location = /path
^~
(forward match)location ^~ /path
~
(regular expression case sensitive)location ~ /path/
~*
(regular expression case insensitive)location ~* .(jpg|png|bmp)
/
location /path
-
8
-
2Leaving off the the trailing slash will match more than just exact. #1 should be
location = /path/
, and the others should include start and end modifiers (^
and$
) Jun 22, 2021 at 17:29 -
location = /path matches to domain.com/path, and location = /path/ to domain.com/path/. Others don't need start and end modifiers. Jun 22, 2021 at 23:19
-
The regular expressions matchers, case-sensitive and insensitive, have same precedence. Correct this information please.– NiloctFeb 24 at 19:00
There is a handy online tool for testing location priority now:
location priority testing online
-
4
Locations are evaluated in this order:
location = /path/file.ext {}
Exact matchlocation ^~ /path/ {}
Priority prefix match -> longest firstlocation ~ /Paths?/ {}
(case-sensitive regexp) andlocation ~* /paths?/ {}
(case-insensitive regexp) -> first matchlocation /path/ {}
Prefix match -> longest first
The priority prefix match (number 2) is exactly as the common prefix match (number 4), but has priority over any regexp.
For both prefix matche types the longest match wins.
Case-sensitive and case-insensitive have the same priority. Evaluation stops at the first matching rule.
Documentation says that all prefix rules are evaluated before any regexp, but if one regexp matches then no standard prefix rule is used. That's a little bit confusing and does not change anything for the priority order reported above.