Bird
Raised Fist0
Nginxdevops~20 mins

Exact 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
🎖️
Exact Match 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 exact match (=) in this nginx location block?
Consider this nginx configuration snippet:
location = /about {
    return 200 'Exact match page';
}

What happens when a client requests /about?
Nginx
location = /about {
    return 200 'Exact match page';
}
AThe server returns 'Exact match page' for any URI starting with '/about'.
BThe server returns 'Exact match page' for any URI containing '/about' anywhere.
CThe server returns 'Exact match page' for any URI ending with '/about'.
DThe server returns 'Exact match page' only if the request URI is exactly '/about'.
Attempts:
2 left
💡 Hint
The '=' sign means the location matches the URI exactly, no more, no less.
🧠 Conceptual
intermediate
2:00remaining
How does nginx prioritize exact match (=) locations compared to prefix matches?
Given these location blocks:
location = /test {
    return 200 'Exact';
}
location /test {
    return 200 'Prefix';
}

Which location block will nginx use for a request to /test?
Nginx
location = /test {
    return 200 'Exact';
}
location /test {
    return 200 'Prefix';
}
Anginx uses the exact match location (= /test) before the prefix match (/test).
Bnginx uses the prefix match (/test) before the exact match (= /test).
Cnginx merges both locations and returns both responses.
Dnginx returns a 404 error because of conflicting locations.
Attempts:
2 left
💡 Hint
Exact match locations have the highest priority in nginx.
Troubleshoot
advanced
2:00remaining
Why does this nginx config not serve the exact match location as expected?
You have this config:
location = /home {
    proxy_pass http://backend1;
}
location /home {
    proxy_pass http://backend2;
}

Requests to /home are always served by backend2. Why?
Nginx
location = /home {
    proxy_pass http://backend1;
}
location /home {
    proxy_pass http://backend2;
}
AThe request URI includes a trailing slash, so it does not exactly match '/home'.
BThe exact match location is ignored because proxy_pass is missing a trailing slash.
Cnginx does not support exact match locations with proxy_pass directives.
DThe exact match location is overridden by the prefix location because of order in the config file.
Attempts:
2 left
💡 Hint
Check if the request URI exactly matches the location string, including trailing slashes.
Best Practice
advanced
2:00remaining
What is the recommended use of exact match (=) locations in nginx?
Which of these is the best practice when using exact match (=) locations in nginx?
AAvoid exact match locations because they cause nginx to reload the config on every request.
BUse exact match locations for specific URIs that need unique handling, like redirects or special responses.
CUse exact match locations for all static files to improve performance.
DUse exact match locations to match all URIs starting with a prefix.
Attempts:
2 left
💡 Hint
Exact match locations are precise and should be used sparingly for special cases.
🔀 Workflow
expert
3:00remaining
Order the steps nginx follows to select a location block for a request URI
Arrange the steps nginx uses to find the correct location block for a request URI, considering exact match (=), prefix, and regex locations.
A1,3,2,4
B2,1,3,4
C1,2,3,4
D3,1,2,4
Attempts:
2 left
💡 Hint
Exact matches have highest priority, then prefix, then regex.

Practice

(1/5)
1. What does the = sign mean in an nginx location block?
easy
A. It matches URLs ending with the given path.
B. It matches the exact URL path only.
C. It matches URLs containing the given path anywhere.
D. It matches any URL starting with the given path.

Solution

  1. Step 1: Understand nginx location matching

    The = sign in nginx location means the URL must match exactly the specified path.
  2. Step 2: Compare with other matching types

    Other types like prefix matching use location /path without =, which matches URLs starting with that path.
  3. Final Answer:

    It matches the exact URL path only. -> Option B
  4. Quick Check:

    Exact match = exact URL [OK]
Hint: Exact match uses = sign, no extra path allowed [OK]
Common Mistakes:
  • Thinking = matches URL prefixes
  • Confusing = with regex matching
  • Assuming = matches URLs containing the path
2. Which of the following is the correct syntax to define an exact match location for URL /about in nginx?
easy
A. location /about { }
B. location /about = { }
C. location ~ /about { }
D. location = /about { }

Solution

  1. Step 1: Recall nginx exact match syntax

    Exact match uses location = /path { } syntax, where = comes immediately after location.
  2. Step 2: Check other options

    location /about = { } places = after path, which is invalid. location ~ /about { } uses regex (~), not exact match. location /about { } is prefix match.
  3. Final Answer:

    location = /about { } -> Option D
  4. Quick Check:

    Exact match syntax = location = /path [OK]
Hint: Put = right after location for exact match [OK]
Common Mistakes:
  • Placing = after the path
  • Using regex (~) instead of = for exact match
  • Omitting = for exact match
3. Given this nginx config snippet:
location = /test {
  return 200 'Exact match';
}
location /test {
  return 200 'Prefix match';
}

What will be the response body when accessing URL /test?
medium
A. Exact match
B. Prefix match
C. 404 Not Found
D. 500 Internal Server Error

Solution

  1. Step 1: Identify matching location for /test

    The URL /test matches exactly the location = /test block because of the exact match sign =.
  2. Step 2: Understand nginx location priority

    Exact match locations have higher priority than prefix matches, so the first block is used and returns 'Exact match'.
  3. Final Answer:

    Exact match -> Option A
  4. Quick Check:

    Exact match location wins for exact URL [OK]
Hint: Exact match location overrides prefix for exact URL [OK]
Common Mistakes:
  • Choosing prefix match response for exact URL
  • Assuming 404 if exact match exists
  • Confusing order of location blocks
4. You wrote this nginx config:
location = /home {
  proxy_pass http://backend;
}
location /home {
  proxy_pass http://frontend;
}

But requests to /home are always sent to http://frontend. What is the likely problem?
medium
A. The exact match location block is not matched because of a syntax error.
B. The exact match location block is missing a trailing slash.
C. The exact match location is overridden by prefix match due to order.
D. The exact match location block is ignored because proxy_pass is invalid.

Solution

  1. Step 1: Check syntax of exact match location

    The exact match location location = /home { ... } may have a syntax error like missing semicolon after proxy_pass, causing nginx to ignore it.
  2. Step 2: Consider nginx matching rules

    Exact match locations have highest priority and should be matched first. If requests go to frontend, likely the exact match block is ignored due to a syntax error or config reload issue.
  3. Step 3: Identify common mistake

    Often, missing semicolons or incorrect proxy_pass URL cause nginx to ignore the block silently.
  4. Final Answer:

    The exact match location block is not matched because of a syntax error. -> Option A
  5. Quick Check:

    Syntax errors cause nginx to ignore blocks [OK]
Hint: Check syntax errors if exact match ignored [OK]
Common Mistakes:
  • Assuming order affects exact match priority
  • Missing semicolon after proxy_pass
  • Confusing trailing slash importance
5. You want to serve a special static page only when the URL is exactly /special. Which nginx config snippet correctly achieves this without affecting other URLs starting with /special?
hard
A. location /special { root /var/www/special; }
B. location ~ /special { root /var/www/special; }
C. location = /special { root /var/www/special; }
D. location ^~ /special { root /var/www/special; }

Solution

  1. Step 1: Understand exact match requirement

    To serve only the exact URL /special, use location = /special which matches exactly that path.
  2. Step 2: Analyze other options

    location /special { root /var/www/special; } matches any URL starting with /special. location ~ /special { root /var/www/special; } uses regex to match URLs containing /special anywhere. location ^~ /special { root /var/www/special; } uses prefix match with ^~, which matches prefixes but not exact only.
  3. Final Answer:

    location = /special { root /var/www/special; } -> Option C
  4. Quick Check:

    Exact match = location = /path [OK]
Hint: Use = for exact URL, not prefix or regex [OK]
Common Mistakes:
  • Using prefix match for exact URL
  • Overcomplicating with regex
  • Confusing ^~ with exact match