0
0
Nginxdevops~10 mins

Prefix match in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Prefix match
Request URL arrives
Check location blocks
Match prefix location?
Serve content or proxy
Nginx checks the request URL against location blocks using prefix matching to find the best matching path prefix.
Execution Sample
Nginx
location /images/ {
    root /data;
}

location / {
    root /var/www/html;
}
This config serves URLs starting with /images/ from /data, others from /var/www/html.
Process Table
StepRequest URLLocation TestedPrefix Match?Action Taken
1/images/cat.png/images/YesSelect this location
2/images/cat.png/YesKeep searching for longer prefix
3/images/cat.pngNo more locationsN/AServe from /images/ location root /data
4/about.html/images/NoSkip this location
5/about.html/YesSelect this location
6/about.htmlNo more locationsN/AServe from / location root /var/www/html
💡 Nginx picks the longest matching prefix location or default location if no prefix matches.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Selected LocationNone/images//images//images//images/
Request URL/images/cat.png/images/cat.png/images/cat.png/images/cat.png/images/cat.png
Key Moments - 2 Insights
Why does nginx continue checking other locations after finding a prefix match?
Nginx looks for the longest prefix match, so even if a prefix matches early (like '/'), it keeps checking for a longer prefix (like '/images/') as shown in steps 1 and 2.
What happens if no prefix matches the request URL?
Nginx uses the default location (usually '/') to serve the request, as shown in steps 4 to 6 for '/about.html'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which location is selected for the URL '/images/cat.png'?
ANo location
B/images/
C/
D/about/
💡 Hint
Check steps 1 to 3 in the execution table where '/images/' is selected as the longest prefix match.
At which step does nginx decide to serve content from the '/' location for '/about.html'?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look at steps 4 to 6 where '/images/' is skipped and '/' is selected finally.
If a new location 'location /images/cat/' is added, how would the execution table change for '/images/cat.png'?
AIt would select '/'
BIt would still select '/images/'
CIt would select '/images/cat/' as the longest prefix match
DIt would cause an error
💡 Hint
Longest prefix match means the most specific prefix wins, so '/images/cat/' would be chosen.
Concept Snapshot
Nginx prefix match:
- Checks request URL against location prefixes
- Chooses longest matching prefix
- If none match, uses default location
- Example: /images/ matches URLs starting with /images/
- Order matters only for regex, prefix matches longest prefix wins
Full Transcript
When nginx receives a request URL, it checks all location blocks to find the best prefix match. It starts by testing each location's prefix against the URL. If a prefix matches, nginx remembers it but continues checking for a longer prefix match. If no longer prefix is found, nginx uses the best match to serve content. If no prefix matches, it uses the default location, usually '/'. For example, a request to '/images/cat.png' matches both '/' and '/images/', but nginx selects '/images/' because it is longer and more specific. This process ensures requests are routed to the correct content folder or proxy target.