0
0
Nginxdevops~10 mins

Nested location blocks in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Nested location blocks
Request comes in
Match outer location block
Yes
Check nested location blocks
Yes
Match most specific nested location
Serve content or proxy
End
When a request arrives, nginx first matches the outer location block, then checks nested locations inside it to find the most specific match before serving content.
Execution Sample
Nginx
server {
  location /app/ {
    root /var/www/html;
    location /app/images/ {
      root /var/www/images;
    }
  }
}
This config shows a nested location where /app/images/ overrides the root for image files inside /app.
Process Table
StepRequest URIOuter Location MatchNested Location CheckNested Location MatchAction Taken
1/app/index.html/app/ matchesCheck nested locationsNo nested matchServe /var/www/html/app/index.html
2/app/images/logo.png/app/ matchesCheck nested locations/app/images/ matchesServe /var/www/images/app/images/logo.png
3/app/css/style.css/app/ matchesCheck nested locationsNo nested matchServe /var/www/html/app/css/style.css
4/other/page.htmlNo outer matchNo nested checkNo nested matchNo match, default server action
💡 Execution stops after serving matched content or default action if no location matches.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Request URI/app/index.html/app/images/logo.png/app/css/style.css/other/page.html
Outer Locationnone/app//app//app/none
Nested Locationnonenone/app/images/nonenone
Root Usednone/var/www/html/var/www/images/var/www/htmldefault
Key Moments - 2 Insights
Why does /app/images/logo.png use a different root than /app/index.html?
Because /app/images/ is a nested location inside /app/ with its own root directive, nginx uses the nested location's root for more specific matches as shown in step 2 of the execution_table.
What happens if a request does not match the outer location block?
Nginx skips nested location checks and uses the default server action, as shown in step 4 where /other/page.html does not match any location.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what root is used for the request /app/css/style.css?
A/var/www/images
B/var/www/html
Cdefault root
DNo root used
💡 Hint
Check step 3 in the execution_table under 'Root Used' column.
At which step does nginx find a nested location match?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Nested Location Match' column in the execution_table.
If the nested location /app/images/ was removed, what would happen to requests for /app/images/logo.png?
AThey would use /var/www/html root
BThey would use /var/www/images root
CThey would not match any location
DThey would cause an error
💡 Hint
Refer to the variable_tracker and execution_table for how nested locations override outer roots.
Concept Snapshot
Nested location blocks in nginx allow more specific matching inside a broader location.
Nginx first matches the outer location, then checks nested locations.
The most specific nested location's settings override outer ones.
If no nested match, outer location settings apply.
Requests not matching outer location skip nested checks.
Use nested locations to organize and override config cleanly.
Full Transcript
When nginx receives a request, it first tries to match the outer location block. If it finds one, it then checks if any nested location blocks inside it match the request more specifically. If a nested location matches, nginx uses its configuration, such as root or proxy settings, to serve the request. If no nested location matches, nginx uses the outer location's settings. If the request does not match the outer location at all, nginx skips nested checks and uses the default server behavior. For example, a request to /app/images/logo.png matches the outer /app/ location and the nested /app/images/ location, so nginx uses the nested root /var/www/images. A request to /app/index.html matches only the outer location, so it uses /var/www/html. This layered matching helps organize configurations clearly and apply specific rules where needed.