API routing with location blocks in Nginx - Time & Space Complexity
When nginx receives a request, it checks location blocks to find the best match. Understanding how this matching grows with more locations helps us see how fast nginx can route requests.
We want to know: how does the time to find the right location change as we add more routes?
Analyze the time complexity of the following nginx location routing snippet.
server {
listen 80;
location /api/v1/ {
proxy_pass http://backend_v1;
}
location /api/v2/ {
proxy_pass http://backend_v2;
}
location /api/ {
proxy_pass http://backend_default;
}
}
This configuration routes requests to different backends based on the URL path prefix.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks each location block in order to find the best match.
- How many times: It compares the request path against each location until it finds the best fit.
As the number of location blocks grows, nginx must check more entries to find the right route.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons |
| 100 | About 100 comparisons |
| 1000 | About 1000 comparisons |
Pattern observation: The number of checks grows roughly in direct proportion to the number of location blocks.
Time Complexity: O(n)
This means the time to find the right location grows linearly as you add more location blocks.
[X] Wrong: "nginx instantly finds the right location no matter how many routes there are."
[OK] Correct: nginx checks locations one by one until it finds the best match, so more routes mean more checks and longer matching time.
Understanding how routing scales helps you design efficient server configs and shows you think about performance in real systems. This skill is useful when working with web servers or APIs.
"What if nginx used a hash map for location matching instead of checking each location in order? How would the time complexity change?"
Practice
location block in an nginx configuration do?Solution
Step 1: Understand the purpose of location blocks
Location blocks in nginx are used to match parts of the URL path to decide how to handle requests.Step 2: Identify what location blocks control
They control routing of requests to different handlers or backends based on the URL.Final Answer:
Routes incoming requests based on URL paths -> Option DQuick Check:
Location blocks = URL routing [OK]
- Confusing location blocks with server IP settings
- Thinking location blocks set server OS
- Assuming location blocks control upload size
/api/v1/users?Solution
Step 1: Recall nginx location modifiers
The=modifier means exact match of the URL path.Step 2: Match the exact path syntax
Usinglocation = /api/v1/usersmatches only that exact path.Final Answer:
location = /api/v1/users { ... } -> Option AQuick Check:
Exact match uses = modifier [OK]
- Using no modifier for exact match
- Using regex (~) when exact match needed
- Using ^~ which is prefix but not exact
location /api/ {
proxy_pass http://backend-service/;
}What happens when a client requests
/api/users?Solution
Step 1: Understand proxy_pass with trailing slash
Whenproxy_passURL ends with a slash, nginx replaces the matching part of the request URI.Step 2: Apply to the request path
The matching prefix/api/is replaced by/from proxy_pass, so/api/usersbecomes/usersat backend.Final Answer:
Request is forwarded to http://backend-service/users -> Option AQuick Check:
proxy_pass with trailing slash replaces prefix [OK]
- Assuming proxy_pass appends full original URI
- Confusing local serving with proxying
- Ignoring trailing slash effect in proxy_pass
location /api {
proxy_pass http://backend-service/;
}Solution
Step 1: Check location path matching behavior
Without a trailing slash,location /apimatches only the exact path/apiand not subpaths like/api/users.Step 2: Correct usage for subpath matching
To match/apiand all subpaths, uselocation /api/with trailing slash.Final Answer:
location path should end with a slash to match subpaths -> Option BQuick Check:
Trailing slash in location matches subpaths [OK]
- Thinking proxy_pass trailing slash is wrong here
- Assuming location without slash matches subpaths
- Believing config is error-free without checking path
/api/v2/ to http://v2-backend/ but requests exactly to /api/v2/status should return a static JSON response locally. Which nginx config achieves this?Solution
Step 1: Use exact match for static response
Uselocation = /api/v2/statusto match exactly that path and return static JSON.Step 2: Use prefix match for all other /api/v2/ requests
Uselocation /api/v2/to proxy all other requests to backend.Final Answer:
location = /api/v2/status { return 200 '{"status":"ok"}'; } location /api/v2/ { proxy_pass http://v2-backend/; } -> Option CQuick Check:
Exact match first, then prefix proxy [OK]
- Placing prefix location before exact match causing override
- Using proxy_pass for static JSON response
- Not escaping JSON properly in return directive
Ready to prove it?
Pass the test and this pattern moves unknown
