How does Nginx match location blocks and what is the priority order?
Learn how Nginx matches location blocks: exact, prefix, ^~ and regex priority order, with config examples and interview questions and answers.
Expected Interview Answer
Nginx does not match location blocks in the order they appear; it follows a fixed priority: exact match (=), then the longest prefix match, with prefix-preference (^~) short-circuiting regex, and finally regex matches (~ and ~*) tested in file order.
For a request URI, Nginx first checks for an exact = match and stops if found. Otherwise it scans all prefix locations and remembers the longest matching one. If that longest prefix uses ^~, Nginx uses it and skips regex entirely. If not, Nginx then evaluates regex locations in the order written and uses the first one that matches; if no regex matches, it falls back to the remembered longest prefix. This is why ordering matters only among regex blocks, not among prefix blocks.
- Predictable routing regardless of block order
- Exact matches let you fast-path hot URLs
- ^~ avoids costly regex evaluation
- Longest-prefix wins prevents accidental shadowing
- Regex order gives fine-grained control when needed
AI Mentor Explanation
Think of a captain choosing a bowler by rules, not by who is nearest. An exact plan for this batter (a known weakness) is picked first. If none, the captain picks the bowler whose strength most fully covers the situation — the longest overlap. Only if that plan allows does he then run through his special variation-bowlers in a fixed order, using the first that fits, mirroring how Nginx resolves location priority.
Step-by-Step Explanation
Step 1
Check exact match
Nginx tests any location = /path first; on a hit it stops immediately and uses that block.
Step 2
Scan prefix matches
It evaluates all prefix (no-modifier and ^~) locations and remembers the one with the longest matching prefix.
Step 3
Honor the ^~ short-circuit
If the longest matching prefix uses ^~, Nginx selects it and skips all regex evaluation.
Step 4
Evaluate regex in order
Otherwise Nginx tests ~ and ~* regex locations in the order they appear and uses the first that matches.
Step 5
Fall back to longest prefix
If no regex matches, Nginx uses the longest prefix location it remembered earlier.
What Interviewer Expects
- Knowing priority is not top-to-bottom order
- Correct ranking of =, ^~, prefix, and regex
- Understanding the ^~ short-circuit behavior
- Awareness that regex is tested in file order
- A concrete example resolving a request URI
Common Mistakes
- Assuming locations are matched in written order
- Confusing ^~ with a regex modifier
- Thinking regex always beats a longer prefix
- Forgetting that = stops matching immediately
- Not knowing case-insensitive ~* exists
Best Answer (HR Friendly)
“Nginx decides which rule handles a web request using a set priority rather than the order the rules are written. It checks for an exact match first, then the most specific matching path, and finally pattern-based rules, so routing stays predictable.”
Code Example
server {
listen 80;
server_name example.com;
# 1. Exact match — highest priority, stops here
location = /login {
return 200 "exact login";
}
# 2. Prefix with ^~ — skips regex if it is the longest match
location ^~ /static/ {
root /var/www;
}
# 3. Regex — tested in order, case-insensitive
location ~* \.(jpg|png|css|js)$ {
expires 30d;
}
# 4. Plain prefix — fallback when no regex matches
location / {
proxy_pass http://app_backend;
}
}Follow-up Questions
- What is the difference between ~ and ~* modifiers?
- When would you use = for a location and why?
- How does ^~ change regex evaluation?
- Does the order of prefix blocks matter?
- How do nested locations affect matching?
MCQ Practice
1. Which location modifier has the highest matching priority in Nginx?
An exact = match is tested first and, on a hit, Nginx stops immediately without checking any other block.
2. What does the ^~ modifier do when it is the longest matching prefix?
If the longest matching prefix uses ^~, Nginx selects it and does not evaluate any regex locations.
3. In what order are regex (~) locations evaluated?
Regex locations are tested in the sequence they are written, and the first one that matches is used.
Flash Cards
Full Nginx location priority order? — Exact (=) → longest prefix, with ^~ short-circuiting → regex (~, ~*) in file order → plain longest prefix fallback.
What does ^~ mean? — A prefix match that, when it is the longest match, skips regex evaluation entirely.
Difference between ~ and ~*? — ~ is a case-sensitive regex match; ~* is case-insensitive.
Does prefix block order matter? — No — the longest matching prefix always wins regardless of position; only regex order matters.