Bird
Raised Fist0
Nginxdevops~20 mins

Prefix match in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does prefix match in nginx do when routing requests?
easy
A. It matches requests based on the start of the URL path.
B. It matches requests only if the full URL matches exactly.
C. It matches requests based on the file extension.
D. It matches requests randomly to any location block.

Solution

  1. Step 1: Understand prefix match concept

    Prefix match means nginx checks if the URL path starts with a specific string.
  2. Step 2: Compare with other matching types

    Exact match requires full URL match, file extension match is unrelated, random match is invalid.
  3. Final Answer:

    It matches requests based on the start of the URL path. -> Option A
  4. Quick Check:

    Prefix match = start of URL path [OK]
Hint: Prefix match checks URL start, not full or random [OK]
Common Mistakes:
  • Confusing prefix match with exact match
  • Thinking prefix match checks file extensions
  • Assuming prefix match is random
2. Which of the following is the correct syntax for a prefix match location in nginx?
easy
A. location = /images/ { }
B. location ^~ /images/ { }
C. location /images/ { }
D. location ~ /images/ { }

Solution

  1. Step 1: Identify prefix match syntax

    Prefix match uses plain location /prefix/ { } without modifiers.
  2. Step 2: Understand other modifiers

    = is exact match, ^~ is prefix but with higher priority, ~ is regex match.
  3. Final Answer:

    location /images/ { } -> Option C
  4. Quick Check:

    Plain location = prefix match [OK]
Hint: Plain location block means prefix match [OK]
Common Mistakes:
  • Using = or ^~ modifiers for simple prefix match
  • Confusing regex (~) with prefix match
  • Missing trailing slash in prefix
3. Given this nginx config snippet:
location /app/ {
  proxy_pass http://backend1;
}
location /app/api/ {
  proxy_pass http://backend2;
}

Which backend will handle a request to /app/api/users?
medium
A. Both backends in round-robin
B. http://backend2
C. No backend, 404 error
D. http://backend1

Solution

  1. Step 1: Understand prefix match selection

    nginx selects the location with the longest matching prefix for plain prefix locations (no modifiers).
  2. Step 2: Compare prefixes

    Both /app/ (length 5) and /app/api/ (length 9) match /app/api/users, but /app/api/ is longer, so it wins.
  3. Final Answer:

    http://backend2 -> Option B
  4. Quick Check:

    Longest prefix wins [OK]
Hint: Longest matching prefix wins [OK]
Common Mistakes:
  • Thinking config order (first match) wins instead of longest prefix
  • Thinking regex or exact match applies here
  • Believing nginx load balances both backends
4. You wrote this nginx config:
location /static {
  root /var/www/html;
}

Requests to /static/css/style.css return 404. What is the likely error?
medium
A. root directive path is incorrect
B. location block should use = modifier
C. Missing trailing slash in location prefix
D. proxy_pass is missing

Solution

  1. Step 1: Understand root directive behavior

    root appends the full request URI (/static/css/style.css) to /var/www/html, looking for /var/www/html/static/css/style.css.
  2. Step 2: Identify likely cause

    If static files are at /var/www/html/css/style.css (without static/), root path doesn't strip prefix, causing 404. (Use alias /var/www/html; to strip.)
  3. Final Answer:

    root directive path is incorrect -> Option A
  4. Quick Check:

    root + full URI (no prefix strip) [OK]
Hint: root appends full URI; alias strips prefix [OK]
Common Mistakes:
  • Confusing root (full URI) with alias (strips prefix)
  • Thinking trailing slash strips prefix for root
  • Adding unnecessary modifiers like =
5. You want nginx to route all requests starting with /api/ to http://backend_api and all others to http://backend_web, ensuring the /api/ prefix has priority even if regex locations follow. Which config correctly uses prefix match for this?
hard
A. location ~ /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; }
B. location /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; }
C. location = /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; }
D. location ^~ /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; }

Solution

  1. Step 1: Use ^~ for prefix priority

    The ^~ modifier tells nginx to stop searching if prefix matches, ensuring /api/ routes to backend_api even over later regex.
  2. Step 2: Default location for others

    The plain location / { } catches all other requests to backend_web.
  3. Final Answer:

    location ^~ /api/ { proxy_pass http://backend_api; } location / { proxy_pass http://backend_web; } -> Option D
  4. Quick Check:

    ^~ prefix match for priority routing [OK]
Hint: Use ^~ to prioritize prefix match over regex [OK]
Common Mistakes:
  • Not using ^~ causes regex or exact match to override
  • Using = modifier limits to exact /api/ only
  • Using ~ makes it regex, not prefix match