0
0
Nginxdevops~20 mins

Preferential prefix match (^~) in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nginx Location Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the effect of the ^~ prefix in this nginx location block?
Consider this nginx configuration snippet:
location ^~ /images/ {
    root /data;
}

What does the ^~ prefix do when a request URL starts with /images/?
AIt matches /images/ only if no exact match is found.
BIt matches the prefix /images/ preferentially and stops searching further regex locations.
CIt treats /images/ as a regex pattern and matches accordingly.
DIt matches /images/ only if the request method is POST.
Attempts:
2 left
💡 Hint
Think about how nginx chooses location blocks when multiple matches exist.
🧠 Conceptual
intermediate
2:00remaining
How does nginx prioritize location blocks with ^~, =, and regex?
Given these location blocks:
location = /exact {
    ...
}
location ^~ /prefix/ {
    ...
}
location ~* \.php$ {
    ...
}

Which order does nginx use to select the matching location for a request?
AExact match (=) first, then ^~ prefix match, then regex (~ or ~*), then normal prefix matches.
BRegex (~ or ~*) first, then exact (=), then ^~ prefix, then normal prefix matches.
CExact (=) and regex (~ or ~*) have equal priority, ^~ prefix is last.
DNormal prefix matches first, then exact (=), then regex (~ or ~*), then ^~ prefix.
Attempts:
2 left
💡 Hint
Remember nginx tries exact matches before anything else.
Troubleshoot
advanced
2:00remaining
Why is the regex location block ignored despite matching the request?
Given this nginx config:
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?
ABecause ^~ prefix match stops regex location checks once matched.
BBecause regex locations have higher priority than ^~ prefix matches.
CBecause proxy_pass directives cannot be used in regex locations.
DBecause the regex location pattern is invalid and ignored.
Attempts:
2 left
💡 Hint
Think about how ^~ affects regex location evaluation.
🔀 Workflow
advanced
2: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.
A2,1,3,4
B1,3,2,4
C1,2,3,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Exact matches are checked first, then ^~, then regex.
Best Practice
expert
2: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?
A
location ~ ^/static/ {
    root /var/www/static;
}
B
location = /static/ {
    root /var/www/static;
}
C
location /static/ {
    root /var/www/static;
}
D
location ^~ /static/ {
    root /var/www/static;
}
Attempts:
2 left
💡 Hint
Use the prefix match that stops regex evaluation.