0
0
Nginxdevops~20 mins

Location matching priority order in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NGINX Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding NGINX location matching priority

In NGINX, multiple location blocks can match a request URI. Which type of location block has the highest priority when matching a request?

AA location with the <code>^~</code> prefix (prefix match with no regex check)
BA location with the <code>~</code> prefix (case-sensitive regex)
CA location with the <code>=</code> prefix (exact match)
DA location without any prefix (standard prefix match)
Attempts:
2 left
💡 Hint

Exact matches are checked before regex or prefix matches.

💻 Command Output
intermediate
2:00remaining
Output of NGINX location matching with multiple blocks

Given this NGINX configuration snippet, what will be the response body when requesting /images/logo.png?

location = /images/logo.png {
  return 200 'Exact match';
}
location ^~ /images/ {
  return 200 'Prefix match';
}
location ~* \.(png|jpg)$ {
  return 200 'Regex match';
}
location / {
  return 200 'Default match';
}
ADefault match
BPrefix match
CRegex match
DExact match
Attempts:
2 left
💡 Hint

Check which location matches exactly the requested URI.

🔀 Workflow
advanced
2:30remaining
Order of NGINX location matching steps

Arrange the following NGINX location matching steps in the correct order of evaluation when a request arrives.

A1,2,3,4
B2,1,3,4
C1,3,2,4
D3,1,2,4
Attempts:
2 left
💡 Hint

Exact matches are checked first, then prefix with ^~, then regex, then standard prefix.

Troubleshoot
advanced
2:30remaining
Why is my regex location not matching in NGINX?

You have these location blocks:

location ^~ /api/ {
  proxy_pass http://backend;
}
location ~ /api/v1/ {
  proxy_pass http://v1-backend;
}

Requests to /api/v1/users are always handled by http://backend. Why?

ABecause regex locations always have higher priority than prefix locations
BBecause <code>^~</code> prefix locations take priority over regex locations
CBecause the regex location is invalid syntax
DBecause <code>proxy_pass</code> is not allowed in regex locations
Attempts:
2 left
💡 Hint

Check the priority order of ^~ vs regex locations.

Best Practice
expert
3:00remaining
Choosing location blocks for performance and clarity

You want to serve static files under /static/ and API requests under /api/. You also have some regex locations for specific API versions. Which configuration approach is best for performance and clarity?

AUse <code>location ^~ /static/</code> for static files, <code>location ^~ /api/</code> for API, and regex locations only for versioned API paths
BUse only regex locations for all paths to have consistent matching
CUse <code>location /</code> for everything and check paths inside application code
DUse <code>location = /static/</code> and <code>location = /api/</code> exact matches only
Attempts:
2 left
💡 Hint

Think about how NGINX prioritizes location blocks and how to avoid unnecessary regex checks.