Route Requests with server and location
🎯 Introduction
One Nginx instance can receive requests for multiple sites and paths. Selection happens in two steps: choose the server, then choose a handling rule within it.
🧱 Select a Server by Address, Port, and Name
A request must first reach the address and port specified by listen. Nginx then matches the HTTP Host request header against server_name. If no name matches, it uses the default server for that listening address and port. See the official request processing documentation.
Create conf/servers/other.conf:
server {
listen 127.0.0.1:8080;
server_name other.test;
location / {
return 200 'other site\n';
}
}
return ends request processing immediately and sends the given status code and content. After reloading, use -H to supply a request header and simulate different domain names:
curl -i -H 'Host: notes.test' http://127.0.0.1:8080/
curl -i -H 'Host: other.test' http://127.0.0.1:8080/
curl -i -H 'Host: unknown.test' http://127.0.0.1:8080/
The second request returns other site; the other two reach the study notes site. This does not change DNS, so entering other.test directly in a browser may still fail. Do not configure two default_server settings for the same address and port.
🧭 Select a location Within the Server
The following rules cover only locations at the same level, without nesting, so you do not apply introductory rules to more complex structures:
| Syntax | Meaning |
|---|---|
location = /health | Matches /health exactly |
location /assets/ | Matches paths starting with /assets/ |
location ^~ /assets/ | A prefix match that skips regular expressions at the same level when selected |
location ~ \.txt$ | Case-sensitive regular expression matching paths ending in .txt |
location ~* \.txt$ | The same regular expression match, ignoring case |
A regular expression describes a text pattern. \. means a literal dot, and $ marks the end. This course uses only a few simple patterns.
Nginx first tries an exact match. Otherwise, it remembers the longest matching prefix. If that prefix has ^~, Nginx uses it directly. Otherwise, it checks regular expressions in configuration order and uses the first match. If none matches, it uses the remembered prefix. Query parameters do not participate in this path matching.
🛠 Verify the Rules with Visible Results
Keep the original location / in the server block in site.conf, and add:
location = /health {
default_type text/plain;
return 200 'healthy\n';
}
location /manual/ {
default_type text/plain;
return 200 'prefix\n';
}
location ~ \.txt$ {
default_type text/plain;
return 200 'regex\n';
}
After reloading, request /health?full=1, /manual/start, and /manual/start.txt. The responses should be healthy, prefix, and regex, respectively. Then add ^~ to /manual/ and reload; the last request should now also return prefix.
/api/ does not match a path that is exactly /api. Handle /api separately if both must work; the proxy chapter shows how.
After the exercise, delete other.conf and remove the /manual/ and .txt regular expression test blocks above. Keep /health. Later exercises access real .txt files, so the test rule must no longer intercept them.
❓ Questions and Answers
Q1: Does placing a plain prefix location first give it priority?
No. Plain prefixes are selected by match length. Only regular expressions use configuration order to select the first match.
Q2: Is server_name _; a special wildcard?
No. This uncommon name is often used for a default server, but the default behavior of listen is what sends unmatched requests there.
🧪 Exercise
Add location = /about returning about page. Test /about, /about?from=home, and /about/.
Check your result: the first two match the exact location; the third does not. Remove the test block afterward to keep the exercise configuration tidy.
🎉 You can now predict request routing from the rules without guessing at configuration order!
