Sub topic 3
Choosing a Server and Matching Locations
This chapter assumes you know basic HTTP requests, headers, TCP ports, and how to edit an Nginx configuration with sudo. The key skill is predicting which server block and which location block handles a request.
How Nginx chooses a server
Nginx first selects a server block using the request's destination IP address, port, and Host header.
It compares the request with:
- The address and port in each
listendirective. - An exact
server_name, such asapi.example.com. - A wildcard name, such as
*.example.com. - A regular expression server name, if one exists.
- The default server for that address and port when no name matches.
The first matching server is the default unless a server explicitly uses default_server.
server {
listen 80 default_server;
server_name _;
return 444;
}
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:4000;
}
}A request for api.example.com uses the third block. A request for an unknown hostname uses the first block and is closed with status 444. The underscore is a convention for a catch-all name. It does not have special meaning.
How Nginx matches locations
After selecting a server, Nginx compares the normalized request URI with its location blocks. The query string is not used for location matching. For example, /images/logo.png?v=2 is matched as /images/logo.png.
The most useful location forms are:
| Form | Meaning | Typical use | Column |
|---|---|---|---|
| `location = /health` | Exact URI match | A health endpoint | |
| `location ^~ /assets/` | Longest prefix, skip regular expressions | Static files | |
| `location /api/` | Prefix match | An application route | |
| `location ~ \.php$` | Case-sensitive regular expression | File patterns | |
| `location ~* \.(jpg | png)$` | Case-insensitive regular expression | Image files |
Nginx generally follows this process:
- It checks exact matches first.
- It finds the longest matching prefix.
- If that prefix uses
^~, Nginx uses it and skips regular expressions. - Otherwise, it tests regular expression locations in configuration order.
- If no regular expression wins, it uses the longest prefix.
server {
listen 80;
server_name example.com;
location = /health {
return 200 "ok\n";
}
location ^~ /assets/ {
root /var/www/site;
}
location ~* \.(jpg|png|css|js)$ {
root /var/www/site;
expires 1h;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}The exact /health block wins over the general / block. The ^~ modifier prevents an image regular expression from unexpectedly taking over requests under /assets/.
Common matching errors
- Omitting
default_servercan send unknown hosts to the first loaded server. - Putting a broad regular expression before a specific one can make the specific rule unreachable.
- Using
/apiwhen you mean only/api/also matches paths such as/application. - Testing a URL with a query string does not test query-string matching. Nginx ignores that string when choosing a location.
- A
Hostvalue with a port, such asexample.com:8080, is still matched against the server name without relying on the port in the name.
Validate changes before reloading:
sudo nginx -t && sudo systemctl reload nginxExercise
Which location handles /assets/logo.png when both location /assets/ and location ~* \.(png|jpg)$ exist, and the prefix location does not use ^~?
Show answer
The regular expression location handles it, because a matching regular expression can override the longest ordinary prefix.