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
Location Matching Priority Order in Nginx
📖 Scenario: You are setting up a simple web server using Nginx. You want to understand how Nginx chooses which location block to use when a user visits different URLs.This is important because different URLs might need different handling, like serving static files or proxying to another server.
🎯 Goal: Build an Nginx configuration with multiple location blocks and learn how Nginx matches URLs to these blocks based on priority rules.You will create a basic server block, add exact and prefix match locations, and then test which location is chosen for a given URL.
📋 What You'll Learn
Create an Nginx server block listening on port 80
Add a prefix location / block
Add an exact match location = /exact block
Add a prefix location /images/ block
Print the matched location block name for a test URL
💡 Why This Matters
🌍 Real World
Web servers often need to serve different content or handle requests differently based on the URL path. Understanding location matching helps configure these behaviors correctly.
💼 Career
DevOps engineers and system administrators frequently configure Nginx for web hosting, load balancing, and reverse proxying. Knowing location matching priority is essential for correct server behavior.
Progress0 / 4 steps
1
Create the basic Nginx server block
Create an Nginx server block listening on port 80 with a root directory set to /var/www/html.
Nginx
Hint
Use listen 80; to listen on port 80 and root /var/www/html; to set the root folder.
2
Add prefix and exact match location blocks
Inside the server block, add a prefix location / block that returns the text Prefix root and an exact match location = /exact block that returns the text Exact match.
Nginx
Hint
Use location / { ... } for prefix match and location = /exact { ... } for exact match.
Use return 200 'text'; to send a simple response.
3
Add a prefix location for images
Add a prefix location /images/ block inside the server that returns the text Images folder.
Nginx
Hint
Use location /images/ { ... } for prefix matching URLs starting with /images/.
4
Test and print matched location for URL /exact
Add a comment inside the server block that explains which location block will match the URL /exact and then print the matched location text by simulating a request to /exact.
Nginx
Hint
The exact match location location = /exact has the highest priority for the URL /exact.
Simulate the output by printing the matched location text.
Practice
(1/5)
1. Which type of location block does nginx check first when matching a request URL?
easy
A. Prefix match without any modifier
B. Exact match using = modifier
C. Regular expression match using ~ or ~*
D. Prefix match with ^~ modifier
Solution
Step 1: Understand nginx location matching order
nginx first looks for an exact match location block marked with =.
Step 2: Confirm priority of exact match
If an exact match is found, nginx immediately uses it without checking other blocks.
Final Answer:
Exact match using = modifier -> Option B
Quick Check:
Exact match = first checked [OK]
Hint: Exact match with = is always checked first [OK]
Common Mistakes:
Thinking prefix matches are checked before exact matches
Confusing ^~ modifier priority
Assuming regex matches are checked first
2. Which of the following is the correct syntax for a location block that uses a case-insensitive regular expression match?
But requests to /images/photo.jpg return 'Prefix match' instead of 'Prefix ^~ match'. What is the likely cause?
medium
A. Two prefix locations with same path cause nginx to pick first declared
B. ^~ modifier is ignored if regex location exists
C. Regex location always has higher priority than ^~
D. Syntax error in ^~ location block
Solution
Step 1: Understand prefix location conflicts
Two prefix locations with identical paths cause nginx to use the first declared one.
Step 2: Check config order
The location /images/ block appears before location ^~ /images/, so nginx picks the first.
Final Answer:
Two prefix locations with same path cause nginx to pick first declared -> Option A
Quick Check:
First prefix wins if paths identical [OK]
Hint: Avoid duplicate prefix locations; nginx picks first declared [OK]
Common Mistakes:
Assuming ^~ always overrides prefix regardless of order
Thinking regex always beats ^~
Believing syntax error causes this behavior
5. You want nginx to serve static files under /static/ using a prefix match but avoid regex checks for these URLs for performance. Which configuration achieves this best?
hard
A. location /static/ { root /var/www/static; }
B. location ~* /static/ { root /var/www/static; }
C. location ^~ /static/ { root /var/www/static; }
D. location = /static/ { root /var/www/static; }
Solution
Step 1: Understand ^~ modifier effect
Using ^~ tells nginx to stop searching regex locations if this prefix matches.
Step 2: Apply to static files
Setting location ^~ /static/ ensures static files served quickly without regex overhead.
Final Answer:
location ^~ /static/ { root /var/www/static; } -> Option C
Quick Check:
^~ prefix disables regex checks [OK]
Hint: Use ^~ prefix to skip regex for performance [OK]