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
Configuring Exact Match (=) in Nginx Location Blocks
📖 Scenario: You are setting up a simple web server using Nginx. You want to serve a special page only when the user visits the exact URL /special. For all other URLs, a default page should be served.
🎯 Goal: Learn how to use the exact match = modifier in Nginx location blocks to serve content only for an exact URL path.
📋 What You'll Learn
Create a basic Nginx server block listening on port 80
Add a location block with exact match = /special to serve a special page
Add a default location / block to serve a default page
Use return directives to send simple text responses
💡 Why This Matters
🌍 Real World
Web servers often need to serve special content for exact URLs, such as landing pages or API endpoints. Using exact match locations ensures precise control over which content is served.
💼 Career
Understanding Nginx location matching is essential for DevOps engineers and system administrators managing web servers and optimizing routing rules.
Progress0 / 4 steps
1
Create the basic server block
Create a server block that listens on port 80 and has a server_name of localhost.
Nginx
Hint
Use server { ... } block with listen 80; and server_name localhost;.
2
Add exact match location for /special
Inside the server block, add a location block with exact match modifier = /special that returns status 200 with the text Special Page.
Nginx
Hint
Use location = /special { return 200 'Special Page'; } inside the server block.
3
Add default location block
Add a default location / block inside the server block that returns status 200 with the text Default Page.
Nginx
Hint
Use location / { return 200 'Default Page'; } inside the server block.
4
Test the configuration output
Print the exact text that Nginx would return when visiting /special and /other URLs using the return directives you configured.
Nginx
Hint
Print Special Page and Default Page on separate lines to simulate Nginx responses.
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
Step 1: Understand nginx location matching
The = sign in nginx location means the URL must match exactly the specified path.
Step 2: Compare with other matching types
Other types like prefix matching use location /path without =, which matches URLs starting with that path.
Final Answer:
It matches the exact URL path only. -> Option B
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
Step 1: Recall nginx exact match syntax
Exact match uses location = /path { } syntax, where = comes immediately after location.
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.
Final Answer:
location = /about { } -> Option D
Quick Check:
Exact match syntax = location = /path [OK]
Hint: Put = right after location for exact match [OK]
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
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.
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.
Step 3: Identify common mistake
Often, missing semicolons or incorrect proxy_pass URL cause nginx to ignore the block silently.
Final Answer:
The exact match location block is not matched because of a syntax error. -> Option A
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
Step 1: Understand exact match requirement
To serve only the exact URL /special, use location = /special which matches exactly that path.
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.
Final Answer:
location = /special { root /var/www/special; } -> Option C
Quick Check:
Exact match = location = /path [OK]
Hint: Use = for exact URL, not prefix or regex [OK]