Challenge - 5 Problems
Nginx Location Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the effect of the ^~ prefix in this nginx location block?
Consider this nginx configuration snippet:
What does the
location ^~ /images/ {
root /data;
}What does the
^~ prefix do when a request URL starts with /images/?Attempts:
2 left
💡 Hint
Think about how nginx chooses location blocks when multiple matches exist.
✗ Incorrect
The ^~ prefix tells nginx to match the given prefix and stop searching further regex locations. This means if the request URI starts with /images/, nginx will use this block immediately without checking regex locations.
🧠 Conceptual
intermediate2:00remaining
How does nginx prioritize location blocks with ^~, =, and regex?
Given these location blocks:
Which order does nginx use to select the matching location for a request?
location = /exact {
...
}
location ^~ /prefix/ {
...
}
location ~* \.php$ {
...
}Which order does nginx use to select the matching location for a request?
Attempts:
2 left
💡 Hint
Remember nginx tries exact matches before anything else.
✗ Incorrect
Nginx first checks for exact matches (=). If none, it checks for ^~ prefix matches. If none, it tests regex locations (~ or ~*). Finally, it uses the longest prefix match without ^~.
❓ Troubleshoot
advanced2:00remaining
Why is the regex location block ignored despite matching the request?
Given this nginx config:
Requests to
location ^~ /api/ {
proxy_pass http://backend;
}
location ~ /api/v[0-9]+/ {
proxy_pass http://versioned-backend;
}Requests to
/api/v2/users are always handled by http://backend. Why?Attempts:
2 left
💡 Hint
Think about how ^~ affects regex location evaluation.
✗ Incorrect
The ^~ prefix tells nginx to stop searching regex locations if the prefix matches. Since /api/v2/users starts with /api/, the ^~ location is chosen and regex is ignored.
🔀 Workflow
advanced2:00remaining
Order the steps nginx follows to select a location block with ^~ and regex
Arrange the steps nginx takes to select a location block when both ^~ and regex locations exist.
Attempts:
2 left
💡 Hint
Exact matches are checked first, then ^~, then regex.
✗ Incorrect
Nginx first looks for exact matches, then ^~ prefix matches, then regex matches, and finally the longest prefix match without ^~.
✅ Best Practice
expert2:00remaining
Which configuration ensures static files under /static/ are served directly, bypassing regex locations?
You want nginx to serve static files under
/static/ directly and not check regex locations. Which location block achieves this?Attempts:
2 left
💡 Hint
Use the prefix match that stops regex evaluation.
✗ Incorrect
Using ^~ prefix match tells nginx to serve /static/ files directly and skip regex locations, ensuring better performance and correct routing.