Sub topic 7
Location Blocks and How Requests Are Matched
Assume you know basic Linux commands, can edit Nginx files with sudo, and understand HTTP paths such as /index.html. You should also know that a server block selects a domain and port before Nginx selects a location block.
What a Location Block Does
A location block matches the URI path in an HTTP request. It controls what Nginx does with that request, such as reading a file, forwarding the request to an application, or returning an error.
For example:
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}A request for /images/logo.png matches location /. A request for /api/users matches location /api/, because that prefix is more specific.
The URI is the path portion of the request. The query string is not used when matching locations. Therefore, /search?q=nginx is matched using /search.
Location Matching Rules
Nginx considers location blocks in a defined order:
- An exact match, written with
=, wins immediately. - Nginx finds the longest matching prefix.
- If that prefix uses
^~, Nginx skips regular expression locations. - Nginx checks regular expression locations in their configuration order. The first matching expression wins.
- If no regular expression matches, Nginx uses the longest prefix.
A regular expression is a pattern that can match many possible strings. The ~ modifier enables a case-sensitive expression. The ~* modifier enables a case-insensitive expression.
| Location | Meaning | Example match | Column |
|---|---|---|---|
| `location = /health` | Exact URI only | `/health` | |
| `location /api/` | Prefix match | `/api/users` | |
| `location ^~ /assets/` | Prefix match that skips regular expressions | `/assets/app.js` | |
| `location ~ \.php$` | Case-sensitive regular expression | `/index.php` | |
| `location ~* \.(jpg | png)$` | Case-insensitive regular expression | `/photo.PNG` |
This example gives an exact health check priority over the general prefix:
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
location = /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
location ^~ /assets/ {
try_files $uri =404;
}
location ~* \.(jpg|png|css|js)$ {
expires 7d;
try_files $uri =404;
}
location / {
try_files $uri $uri/ =404;
}
}The ^~ /assets/ block prevents the regular expression for file extensions from taking over requests under /assets/. Without ^~, a request such as /assets/app.js could be handled by the later regular expression instead.
Common Errors
A prefix location does not automatically match only one directory. location /api also matches /apianalytics. Use location /api/ when the trailing slash matters.
A regular expression is not automatically the most specific rule. Its position among regular expression locations matters. Put more specific expressions before broader ones.
After editing a configuration, test it before reloading:
sudo nginx -t && sudo systemctl reload nginxExercise
Which location handles /assets/site.css when both location ^~ /assets/ and location ~* \.css$ exist?
Show answer
The ^~ /assets/ location handles it because that modifier prevents Nginx from checking regular expression locations.