0
0
Nginxdevops~10 mins

Location matching priority order in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Location matching priority order
Request URL arrives
Check exact match (=)
Use exact location
No
Check prefix match (^~)
Use prefix location
No
Check regex matches (~ or ~*)
Use first regex match
No
Use longest prefix match
Serve request with matched location
Nginx checks locations in this order: exact match first, then prefix with ^~, then regex, and finally longest prefix match.
Execution Sample
Nginx
location = /exact {
  # exact match block
}

location ^~ /images/ {
  # prefix match block
}

location ~* \.jpg$ {
  # regex match block
}

location / {
  # default prefix match
}
This config shows different location blocks with exact, prefix, regex, and default prefix matches.
Process Table
StepCheck TypeConditionResultAction Taken
1Exact match (=)Does request URL exactly match /exact?NoContinue to next check
2Exact match (=)Does request URL exactly match /exact?YesUse exact location block
3Prefix match (^~)Does request URL start with /images/?YesUse prefix location block
4Regex match (~ or ~*)Does request URL match regex \.jpg$?YesUse regex location block
5Longest prefix matchNo exact, ^~, or regex match foundYesUse longest prefix location block
6ExitRequest served with matched locationN/AStop checking
💡 Execution stops when a matching location is found or after longest prefix match is used.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
matched_locationnonenone/exact (if matched)none or /images/ (if matched)none or regex matchfinal matched location
Key Moments - 3 Insights
Why does nginx check exact match locations before prefix or regex?
Because exact matches (=) have the highest priority and stop further checks immediately, as shown in execution_table step 2.
What happens if a prefix match with ^~ is found?
Nginx uses that prefix location immediately without checking regex locations, as shown in execution_table step 3.
When does nginx use regex location blocks?
Only if no exact or ^~ prefix matches are found, nginx tests regex locations in order and uses the first match, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does nginx stop checking if an exact match is found?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'Action Taken' column at step 2 in the execution_table.
If a request URL starts with /images/ but also matches a regex, which location block does nginx use?
APrefix location block with ^~
BExact match location block
CRegex location block
DLongest prefix match location
💡 Hint
Refer to execution_table step 3 where prefix ^~ match takes priority over regex.
According to variable_tracker, what is the value of matched_location after step 4 if no exact or ^~ prefix matches were found but regex matched?
A/exact
Bregex match location
Cnone
Dlongest prefix location
💡 Hint
Look at the 'matched_location' row in variable_tracker after step 4.
Concept Snapshot
Nginx location matching order:
1. Exact match (=) has highest priority.
2. Prefix match with ^~ stops regex checks.
3. Regex (~ or ~*) matches next.
4. Longest prefix match used last.
Request served by first matched location.
Full Transcript
Nginx processes incoming requests by checking location blocks in a specific order. First, it looks for an exact match using '='. If found, it immediately uses that location. If not, it checks for prefix matches marked with '^~', which also stop further regex checks if matched. If neither exact nor ^~ prefix matches are found, nginx tests regex locations in order and uses the first regex match. If no regex matches, nginx falls back to the longest prefix match. This priority ensures efficient and predictable request routing.