Bird
Raised Fist0
Nginxdevops~10 mins

Why location matching controls request routing in Nginx - Visual Breakdown

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Why location matching controls request routing
Incoming HTTP Request
Check location blocks in order
Match request URI with location pattern?
Select matched location
Route request to matched location handler
Response
Nginx checks each location block to find the best match for the request URI, then routes the request accordingly.
Execution Sample
Nginx
location /images/ {
    root /data;
}

location / {
    proxy_pass http://backend;
}
This config routes requests starting with /images/ to serve files from /data, others proxy to backend.
Process Table
StepRequest URILocation Block CheckedMatch ResultRouting Decision
1/images/logo.pnglocation /images/Yes (prefix match)Serve from /data/images/logo.png
2/images/logo.pnglocation /No (already matched)No action
3/api/datalocation /images/NoCheck next location
4/api/datalocation /Yes (prefix match)Proxy to http://backend
5/api/dataEnd of location blocksRouting decidedRequest routed to backend
💡 Routing stops after the best matching location is found and request is routed accordingly.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
Request URI/images/logo.png or /api/data/images/logo.png/api/data/api/data/api/data
Matched LocationNone/images/None//
Routing DecisionNoneServe from /dataNoneProxy to backendProxy to backend
Key Moments - 2 Insights
Why does nginx choose the /images/ location for /images/logo.png instead of the / location?
Because /images/ is a more specific prefix match than /, nginx picks the longest matching prefix first as shown in execution_table step 1.
What happens if no location matches the request URI?
Nginx uses the default location / block if present, or returns a 404 error. This is implied in execution_table step 5 where routing ends after checking all locations.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what routing decision is made for the request URI '/images/logo.png' at step 1?
AProxy to backend server
BServe file from /data directory
CReturn 404 Not Found
DCheck next location block
💡 Hint
Refer to execution_table row with Step 1 under Routing Decision column.
At which step does nginx decide to proxy the request '/api/data' to the backend?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Check execution_table rows for '/api/data' and look at Routing Decision column.
If the location /images/ block was removed, what would happen to the request '/images/logo.png'?
AIt would be served from /data directory
BIt would return 404 Not Found
CIt would be proxied to backend
DIt would cause a server error
💡 Hint
Without /images/ location, nginx matches the next best prefix / as shown in variable_tracker and execution_table.
Concept Snapshot
Nginx routes requests by matching URI to location blocks.
It picks the longest prefix match first.
Requests are handled by the matched location's directives.
If no match, default location or 404 applies.
Order and specificity control routing decisions.
Full Transcript
When nginx receives a request, it checks each location block in the configuration to find the best match for the request URI. It prefers the longest matching prefix. For example, a request to /images/logo.png matches the location /images/ before the generic / location. Once matched, nginx routes the request according to that location's rules, such as serving static files or proxying to a backend. If no location matches, nginx uses the default location or returns a 404 error. This matching controls how requests are routed and handled.

Practice

(1/5)
1. What does the location directive in nginx control?
easy
A. How nginx routes incoming requests based on URL patterns
B. The server's IP address configuration
C. The database connection settings
D. The logging format for errors

Solution

  1. Step 1: Understand the role of location in nginx

    The location directive defines how nginx matches URLs to decide where to send requests.
  2. Step 2: Identify what location controls

    It controls routing of requests, not server IP, database, or logging.
  3. Final Answer:

    How nginx routes incoming requests based on URL patterns -> Option A
  4. Quick Check:

    Location controls routing = B [OK]
Hint: Location matches URLs to route requests [OK]
Common Mistakes:
  • Confusing location with server IP settings
  • Thinking location controls database connections
  • Assuming location affects logging formats
2. Which of the following is the correct syntax to define a prefix location block in nginx?
easy
A. location ~ /example { }
B. location = /example { }
C. location /example { }
D. location ! /example { }

Solution

  1. Step 1: Review nginx location syntax

    Prefix locations use location /path { } without modifiers.
  2. Step 2: Identify correct prefix syntax

    location /example { } is correct for prefix matching.
  3. Final Answer:

    location /example { } -> Option C
  4. Quick Check:

    Prefix location syntax = A [OK]
Hint: Prefix location has no modifier, just path [OK]
Common Mistakes:
  • Using '=' which means exact match, not prefix
  • Using '~' which means regex match
  • Using '!' which is invalid syntax
3. Given this nginx config snippet:
location /images/ {
  root /data;
}
location /images/thumbnails/ {
  root /thumbs;
}

Which root directory will nginx use for the request /images/thumbnails/pic.jpg?
medium
A. Default root directory
B. /data
C. Both /data and /thumbs
D. /thumbs

Solution

  1. Step 1: Understand location matching order

    nginx chooses the most specific matching location block for the URL.
  2. Step 2: Compare matching locations for /images/thumbnails/pic.jpg

    Both /images/ and /images/thumbnails/ match, but /images/thumbnails/ is more specific.
  3. Final Answer:

    /thumbs -> Option D
  4. Quick Check:

    Most specific location wins = A [OK]
Hint: Longest matching prefix location is chosen [OK]
Common Mistakes:
  • Choosing the first matching location instead of the most specific
  • Assuming both roots apply simultaneously
  • Ignoring location specificity
4. You have these location blocks:
location /app/ {
  proxy_pass http://backend1;
}
location ~ /app/ {
  proxy_pass http://backend2;
}

Requests to /app/test always go to backend1. Why?
medium
A. Requests to /app/test do not match either location
B. Prefix locations have higher priority than regex locations
C. The config has a syntax error
D. Regex locations always override prefix locations

Solution

  1. Step 1: Recall nginx location matching priority

    nginx first matches prefix locations, then regex locations only if no prefix matches.
  2. Step 2: Analyze given config

    Since /app/ prefix matches /app/test, nginx uses that before regex ~ /app/.
  3. Final Answer:

    Prefix locations have higher priority than regex locations -> Option B
  4. Quick Check:

    Prefix beats regex if prefix matches = D [OK]
Hint: Prefix location matches first, regex only if no prefix match [OK]
Common Mistakes:
  • Thinking regex always overrides prefix
  • Assuming syntax error causes this
  • Believing request doesn't match any location
5. You want requests to /api/v1/ to go to backend_v1 and requests to /api/ to go to backend_default. Which config correctly routes requests?
hard
A. location /api/v1/ { proxy_pass http://backend_v1; } location /api/ { proxy_pass http://backend_default; }
B. location ~ /api/ { proxy_pass http://backend_v1; } location /api/ { proxy_pass http://backend_default; }
C. location /api/ { proxy_pass http://backend_default; } location ~ /api/v1/ { proxy_pass http://backend_v1; }
D. location = /api/v1/ { proxy_pass http://backend_v1; } location /api/ { proxy_pass http://backend_default; }

Solution

  1. Step 1: Understand location matching order

    nginx chooses the most specific prefix location for a request.
  2. Step 2: Use more specific prefix /api/v1/

    Since /api/v1/ is longer/more specific than /api/, nginx selects it for matching requests.
  3. Step 3: Check options

    location /api/v1/ { proxy_pass http://backend_v1; } location /api/ { proxy_pass http://backend_default; } uses prefix locations where the longer one wins, correctly routing requests.
  4. Final Answer:

    location /api/v1/ { proxy_pass http://backend_v1; } location /api/ { proxy_pass http://backend_default; } -> Option A
  5. Quick Check:

    Specific location before general = C [OK]
Hint: Place more specific location before general prefix [OK]
Common Mistakes:
  • Placing general location before specific causing wrong routing
  • Using regex unnecessarily for simple prefix matching
  • Using exact match which only matches exact URL