0
0
Nginxdevops~10 mins

Location blocks in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Location blocks
Request comes in
Check location blocks in order
Match exact location?
YesUse exact block
No
Match prefix location?
YesUse longest prefix block
No
Match regex location?
YesUse first regex match
No
Use default location block
Serve request with chosen block
Nginx checks location blocks in a specific order: exact match first, then prefix matches, then regex matches, and finally the default block if no match is found.
Execution Sample
Nginx
location = /exact {
  return 200 'Exact match';
}

location /prefix {
  return 200 'Prefix match';
}

location ~ ^/regex {
  return 200 'Regex match';
}
This config defines three location blocks: exact match for /exact, prefix match for /prefix, and regex match for URLs starting with /regex.
Process Table
StepRequest URLCheckMatch ResultChosen Location BlockAction
1/exactCheck exact match location = /exactMatched/exactReturn 200 'Exact match'
2/prefix/testCheck exact match location = /prefix/testNo matchN/AContinue
3/prefix/testCheck prefix location /prefixMatched (longest prefix)/prefixReturn 200 'Prefix match'
4/regex123Check exact match location = /regex123No matchN/AContinue
5/regex123Check prefix location /regex123No matchN/AContinue
6/regex123Check regex location ~ ^/regexMatched/regexReturn 200 'Regex match'
7/otherCheck exact match location = /otherNo matchN/AContinue
8/otherCheck prefix location /otherNo matchN/AContinue
9/otherCheck regex location ~ ^/regexNo matchN/AContinue
10/otherUse default location blockDefault/Serve default content
💡 Request served by the first matching location block found in the order: exact, prefix, regex, or default.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 6After Step 10
Chosen Location BlockNone/exact/prefix/regex/
Key Moments - 3 Insights
Why does nginx choose the exact match location block before prefix or regex?
Nginx prioritizes exact matches first as shown in execution_table row 1, so if an exact match exists, it uses that block immediately.
What happens if no exact or prefix location matches but a regex matches?
Nginx then uses the first regex location that matches, as seen in execution_table row 6 where regex match is chosen after no exact or prefix match.
Why is there a default location block used at the end?
If no exact, prefix, or regex matches are found, nginx falls back to the default location block to serve the request, as in execution_table row 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which location block is chosen for the URL '/prefix/test'?
A/prefix
B/regex
C/exact
D/
💡 Hint
Check execution_table row 3 where prefix match is found for '/prefix/test'.
At which step does nginx decide to use the regex location block?
AStep 1
BStep 3
CStep 6
DStep 10
💡 Hint
See execution_table row 6 where regex match is confirmed.
If the exact location block for '/exact' was removed, what would happen for the URL '/exact'?
AUse regex location if matches
BUse prefix location if matches
CUse default location block
DReturn 404 error
💡 Hint
Refer to concept_flow and execution_table logic: exact match missing leads to prefix match check.
Concept Snapshot
Nginx Location Blocks:
- Exact match (=) checked first
- Then longest prefix match (/)
- Then regex (~ or ~*) matches
- Finally default location (/)
Request served by first matching block found in this order.
Full Transcript
When nginx receives a request, it looks for a matching location block to decide how to serve it. It first checks if there is an exact match location block for the request URL. If found, it uses that block immediately. If not, it looks for prefix matches and chooses the longest matching prefix. If no prefix matches, it checks regex location blocks in order and uses the first regex that matches. If none of these match, it falls back to the default location block. This order ensures nginx serves requests efficiently and predictably.