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.
location /images/ {
root /data;
}
location / {
root /var/www/html;
}| Step | Request URL | Location Tested | Prefix Match? | Action Taken |
|---|---|---|---|---|
| 1 | /images/cat.png | /images/ | Yes | Select this location |
| 2 | /images/cat.png | / | Yes | Keep searching for longer prefix |
| 3 | /images/cat.png | No more locations | N/A | Serve from /images/ location root /data |
| 4 | /about.html | /images/ | No | Skip this location |
| 5 | /about.html | / | Yes | Select this location |
| 6 | /about.html | No more locations | N/A | Serve from / location root /var/www/html |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| Selected Location | None | /images/ | /images/ | /images/ | /images/ |
| Request URL | /images/cat.png | /images/cat.png | /images/cat.png | /images/cat.png | /images/cat.png |
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