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
Prefix Match in Nginx Configuration
📖 Scenario: You are setting up a simple web server using Nginx. You want to serve different content based on the URL path prefix.For example, requests starting with /images should serve image files, and requests starting with /videos should serve video files.
🎯 Goal: Configure Nginx to use prefix matching in location blocks to serve different content based on URL path prefixes.
📋 What You'll Learn
Create an Nginx configuration file with a server block
Add a location block that matches the prefix /images
Add a location block that matches the prefix /videos
Set the root directory for each prefix to /var/www/images and /var/www/videos respectively
Print the final Nginx configuration
💡 Why This Matters
🌍 Real World
Web servers often serve different types of content from different folders. Using prefix matching in Nginx helps route requests to the right content folder.
💼 Career
Understanding Nginx configuration and prefix matching is essential for DevOps roles managing web servers and deploying web applications.
Progress0 / 4 steps
1
Create the basic Nginx server block
Create a variable called nginx_config and assign it a string containing the basic Nginx server block with listen 80; and an empty body for server.
Nginx
Hint
Start with a string that contains server { and listen 80; lines.
2
Add a location block for the /images prefix
Add a location /images/ block inside the server block in nginx_config string. Set its alias to /var/www/images/. Use string concatenation or multiline string to add this block.
Nginx
Hint
Inside the server block, add location /images/ { alias /var/www/images/; }.
3
Add a location block for the /videos prefix
Add a location /videos/ block inside the server block in nginx_config string. Set its alias to /var/www/videos/. Append this block after the /images block.
Nginx
Hint
Append location /videos/ { alias /var/www/videos/; } inside the server block.
4
Print the final Nginx configuration
Write a print statement to display the content of nginx_config.
Nginx
Hint
Use print(nginx_config) to show the full configuration.
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
Step 1: Understand prefix match concept
Prefix match means nginx checks if the URL path starts with a specific string.
Step 2: Compare with other matching types
Exact match requires full URL match, file extension match is unrelated, random match is invalid.
Final Answer:
It matches requests based on the start of the URL path. -> Option A
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
Step 1: Identify prefix match syntax
Prefix match uses plain location /prefix/ { } without modifiers.
Step 2: Understand other modifiers
= is exact match, ^~ is prefix but with higher priority, ~ is regex match.
Final Answer:
location /images/ { } -> Option C
Quick Check:
Plain location = prefix match [OK]
Hint: Plain location block means prefix match [OK]
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
Step 1: Understand prefix match selection
nginx selects the location with the longest matching prefix for plain prefix locations (no modifiers).
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.
Final Answer:
http://backend2 -> Option B
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
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.
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.)
Final Answer:
root directive path is incorrect -> Option A
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?