Challenge - 5 Problems
Prefix Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Nginx prefix match location block behavior
Given the following Nginx configuration snippet, what will be the output when accessing
http://example.com/app/test/?Nginx
server {
listen 80;
server_name example.com;
location /app/ {
return 200 "Prefix match location";
}
location = /app/test {
return 200 "Exact match location";
}
}Attempts:
2 left
💡 Hint
Nginx chooses the longest prefix match location block if no exact match is found.
✗ Incorrect
The location /app/ is a prefix match and matches the request URI /app/test/. The location = /app/test is an exact match but it requires the URI to be exactly /app/test without trailing characters. Since the request URI is /app/test/ (with trailing slash), the prefix match location is chosen.
🧠 Conceptual
intermediate1:30remaining
Understanding Nginx prefix match priority
Which statement correctly describes how Nginx selects a prefix match location block when multiple prefix matches exist?
Attempts:
2 left
💡 Hint
Think about which prefix is more specific.
✗ Incorrect
Nginx chooses the prefix location with the longest matching prefix to serve the request, as it is the most specific match.
❓ Configuration
advanced2:30remaining
Configuring Nginx prefix match for nested paths
You want Nginx to serve requests to
/api/v1/ and all its subpaths with a specific location block. Which configuration snippet correctly uses prefix match to achieve this?Attempts:
2 left
💡 Hint
Prefix match locations use a simple path without modifiers.
✗ Incorrect
The location /api/v1/ is a prefix match and will match /api/v1/ and all subpaths like /api/v1/users. The = modifier is for exact match, ^~ is for prefix but with higher priority, and ~ is for regex match.
❓ Troubleshoot
advanced2:00remaining
Unexpected location block selection in Nginx
Given this Nginx config, why does a request to
/app/test/ get served by the /app/test/ location block instead of /app/?Nginx
location /app/ {
return 200 "App prefix";
}
location /app/test/ {
return 200 "App test prefix";
}Attempts:
2 left
💡 Hint
Consider prefix length and specificity.
✗ Incorrect
Nginx chooses the location with the longest matching prefix. /app/test/ is longer and more specific than /app/, so it is selected for requests starting with /app/test/.
✅ Best Practice
expert3:00remaining
Optimizing Nginx prefix match with ^~ modifier
Which configuration best ensures that requests starting with
/static/ are served directly by Nginx without checking regex locations?Attempts:
2 left
💡 Hint
The ^~ modifier tells Nginx to stop searching regex locations if this prefix matches.
✗ Incorrect
Using ^~ with a prefix location tells Nginx to use this location immediately if the prefix matches, skipping regex checks. This improves performance for static content.