Location matching priority order in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When nginx receives a request, it must decide which location block to use. This decision involves checking multiple location patterns.
We want to understand how the time to find the right location grows as the number of location blocks increases.
Analyze the time complexity of this nginx location matching snippet.
server {
location = /exact {
# exact match
}
location ^~ /prefix {
# prefix match
}
location ~* \.(gif|jpg)$ {
# regex match
}
location / {
# default catch-all
}
}
This snippet shows different types of location blocks nginx checks to find the best match for a request URL.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks location blocks in a specific order: exact matches first, then prefix matches, then regex matches.
- How many times: It checks exact and prefix matches quickly, but regex matches may require testing multiple patterns one by one.
As the number of location blocks grows, nginx spends more time checking regex locations because it tests them one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 regex tests in worst case |
| 100 | About 100 regex tests in worst case |
| 1000 | About 1000 regex tests in worst case |
Pattern observation: The time grows roughly in direct proportion to the number of regex locations.
Time Complexity: O(n)
This means the time to find the matching location grows linearly with the number of regex location blocks.
[X] Wrong: "nginx checks all location blocks equally fast regardless of type."
[OK] Correct: Exact and prefix matches use fast lookups, but regex matches are tested one by one, which takes more time as their number grows.
Understanding how nginx matches locations helps you reason about request routing efficiency and server performance in real projects.
"What if we removed all regex locations? How would the time complexity of location matching change?"
Practice
location block does nginx check first when matching a request URL?Solution
Step 1: Understand nginx location matching order
nginx first looks for an exact match location block marked with=.Step 2: Confirm priority of exact match
If an exact match is found, nginx immediately uses it without checking other blocks.Final Answer:
Exact match using = modifier -> Option BQuick Check:
Exact match = first checked [OK]
- Thinking prefix matches are checked before exact matches
- Confusing ^~ modifier priority
- Assuming regex matches are checked first
Solution
Step 1: Identify regex modifiers in nginx
Modifier~*means case-insensitive regex match.Step 2: Match syntax to case-insensitive regex
location ~* "/images/" { } uses~*correctly for case-insensitive regex.Final Answer:
location ~* "/images/" { } -> Option DQuick Check:
~* = case-insensitive regex [OK]
- Using ~ instead of ~* for case-insensitive match
- Confusing ^~ with regex modifiers
- Using = for regex match
/images/logo.png?
location = /images/logo.png { return 200 'Exact match'; }
location ^~ /images/ { return 200 'Prefix ^~ match'; }
location ~* \.(png|jpg)$ { return 200 'Regex match'; }
location / { return 200 'Default prefix match'; }Solution
Step 1: Check for exact match
Request URL is/images/logo.png, which exactly matches= /images/logo.png.Step 2: Confirm nginx uses exact match first
nginx uses exact match location immediately without checking others.Final Answer:
Exact match location = /images/logo.png -> Option AQuick Check:
Exact match wins over prefix and regex [OK]
- Choosing ^~ prefix match over exact match
- Assuming regex match has higher priority
- Ignoring exact match syntax
location /images/ {
return 200 'Prefix match';
}
location ^~ /images/ {
return 200 'Prefix ^~ match';
}
But requests to /images/photo.jpg return 'Prefix match' instead of 'Prefix ^~ match'. What is the likely cause?Solution
Step 1: Understand prefix location conflicts
Two prefix locations with identical paths cause nginx to use the first declared one.Step 2: Check config order
Thelocation /images/block appears beforelocation ^~ /images/, so nginx picks the first.Final Answer:
Two prefix locations with same path cause nginx to pick first declared -> Option AQuick Check:
First prefix wins if paths identical [OK]
- Assuming ^~ always overrides prefix regardless of order
- Thinking regex always beats ^~
- Believing syntax error causes this behavior
/static/ using a prefix match but avoid regex checks for these URLs for performance. Which configuration achieves this best?Solution
Step 1: Understand ^~ modifier effect
Using^~tells nginx to stop searching regex locations if this prefix matches.Step 2: Apply to static files
Settinglocation ^~ /static/ensures static files served quickly without regex overhead.Final Answer:
location ^~ /static/ { root /var/www/static; } -> Option CQuick Check:
^~ prefix disables regex checks [OK]
- Using plain prefix allows regex checks
- Using regex location unnecessarily
- Using exact match = for directory prefix
