Prefix match in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to find a prefix match in nginx changes as the number of prefixes grows.
How does nginx handle many prefix rules efficiently?
Analyze the time complexity of the following nginx prefix match configuration.
location /images/ {
# serve images
}
location /images/thumbnails/ {
# serve thumbnails
}
location /images/icons/ {
# serve icons
}
This snippet shows multiple prefix locations nginx checks to find the best match for a request URL.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx compares the request URI against each prefix location.
- How many times: It checks prefixes one by one until it finds the longest matching prefix.
As the number of prefix locations increases, nginx must check more prefixes to find the best match.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prefix checks |
| 100 | About 100 prefix checks |
| 1000 | About 1000 prefix checks |
Pattern observation: The number of checks grows linearly with the number of prefix locations.
Time Complexity: O(n)
This means the time to find a prefix match grows directly with how many prefix rules there are.
[X] Wrong: "nginx finds prefix matches instantly no matter how many prefixes exist."
[OK] Correct: nginx checks prefixes one by one, so more prefixes mean more checks and longer time.
Understanding how prefix matching scales helps you explain real-world server behavior clearly and confidently.
"What if nginx used a tree structure to store prefixes? How would the time complexity change?"
Practice
Solution
Step 1: Understand prefix match concept
Prefix match means nginx checks if the URL path starts with a specific string.Step 2: Compare with other matching types
Exact match requires full URL match, file extension match is unrelated, random match is invalid.Final Answer:
It matches requests based on the start of the URL path. -> Option AQuick Check:
Prefix match = start of URL path [OK]
- Confusing prefix match with exact match
- Thinking prefix match checks file extensions
- Assuming prefix match is random
Solution
Step 1: Identify prefix match syntax
Prefix match uses plainlocation /prefix/ { }without modifiers.Step 2: Understand other modifiers
=is exact match,^~is prefix but with higher priority,~is regex match.Final Answer:
location /images/ { } -> Option CQuick Check:
Plain location = prefix match [OK]
- Using = or ^~ modifiers for simple prefix match
- Confusing regex (~) with prefix match
- Missing trailing slash in prefix
location /app/ {
proxy_pass http://backend1;
}
location /app/api/ {
proxy_pass http://backend2;
}Which backend will handle a request to
/app/api/users?Solution
Step 1: Understand prefix match selection
nginx selects the location with the longest matching prefix for plain prefix locations (no modifiers).Step 2: Compare prefixes
Both/app/(length 5) and/app/api/(length 9) match/app/api/users, but/app/api/is longer, so it wins.Final Answer:
http://backend2 -> Option BQuick Check:
Longest prefix wins [OK]
- Thinking config order (first match) wins instead of longest prefix
- Thinking regex or exact match applies here
- Believing nginx load balances both backends
location /static {
root /var/www/html;
}Requests to
/static/css/style.css return 404. What is the likely error?Solution
Step 1: Understand root directive behavior
rootappends the full request URI (/static/css/style.css) to/var/www/html, looking for/var/www/html/static/css/style.css.Step 2: Identify likely cause
If static files are at/var/www/html/css/style.css(withoutstatic/), root path doesn't strip prefix, causing 404. (Usealias /var/www/html;to strip.)Final Answer:
root directive path is incorrect -> Option AQuick Check:
root + full URI (no prefix strip) [OK]
- Confusing root (full URI) with alias (strips prefix)
- Thinking trailing slash strips prefix for root
- Adding unnecessary modifiers like =
/api/ to http://backend_api and all others to http://backend_web, ensuring the /api/ prefix has priority even if regex locations follow. Which config correctly uses prefix match for this?Solution
Step 1: Use ^~ for prefix priority
The^~modifier tells nginx to stop searching if prefix matches, ensuring /api/ routes to backend_api even over later regex.Step 2: Default location for others
The plainlocation / { }catches all other requests to backend_web.Final Answer:
location ^~ /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; } -> Option DQuick Check:
^~ prefix match for priority routing [OK]
- Not using ^~ causes regex or exact match to override
- Using = modifier limits to exact /api/ only
- Using ~ makes it regex, not prefix match
