0
0
Nginxdevops~20 mins

Prefix match in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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";
    }
}
AExact match location
B500 Internal Server Error
C404 Not Found
DPrefix match location
Attempts:
2 left
💡 Hint
Nginx chooses the longest prefix match location block if no exact match is found.
🧠 Conceptual
intermediate
1:30remaining
Understanding Nginx prefix match priority
Which statement correctly describes how Nginx selects a prefix match location block when multiple prefix matches exist?
ANginx selects the prefix location with the shortest matching prefix.
BNginx selects the prefix location with the longest matching prefix.
CNginx selects the first prefix location defined in the config file.
DNginx randomly selects one of the matching prefix locations.
Attempts:
2 left
💡 Hint
Think about which prefix is more specific.
Configuration
advanced
2: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?
Alocation /api/v1/ { proxy_pass http://backend; }
Blocation ^~ /api/v1/ { proxy_pass http://backend; }
Clocation = /api/v1/ { proxy_pass http://backend; }
Dlocation ~ /api/v1/ { proxy_pass http://backend; }
Attempts:
2 left
💡 Hint
Prefix match locations use a simple path without modifiers.
Troubleshoot
advanced
2: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";
}
ABecause /app/test/ is a longer prefix match than /app/, so it is chosen.
BBecause Nginx always prefers the first location block defined.
CBecause /app/ is an exact match and /app/test/ is not.
DBecause the trailing slash in /app/test/ makes it an exact match.
Attempts:
2 left
💡 Hint
Consider prefix length and specificity.
Best Practice
expert
3: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?
Alocation ~ /static/ { root /var/www/static; }
Blocation /static/ { root /var/www/static; }
Clocation ^~ /static/ { root /var/www/static; }
Dlocation = /static/ { root /var/www/static; }
Attempts:
2 left
💡 Hint
The ^~ modifier tells Nginx to stop searching regex locations if this prefix matches.