A server_name list tells Nginx which server block should handle a request for a given Host header. Exact names, wildcards, and regex names are all allowed.
Match order: exact name, then longest leftmost wildcard (*.example.org), then longest rightmost wildcard (mail.*), then the first matching regex.
Config
Start with this shape. Adjust paths for your server.
Nginx
server {
listen 80;
server_name example.org www.example.org;
root /sites/example;
}
server {
listen 80;
server_name example.net www.example.net;
root /sites/net;
}
If no name matches, Nginx uses the default server for that listen port — usually the first server, or the one marked default_server.
Key Directives
Name
Plain meaning
server_name
Host names this server block answers.
default_server
listen parameter that marks the fallback server for a port.
*.example.org
Wildcard matching one or more labels before example.org.
~^www\d+\.example\.net$
Regex name — must start with ~.
Examples
Example 1 — Exact Names
List every hostname you want this site to answer.
Nginx
server {
listen 80;
server_name example.org www.example.org;
root /var/www/example;
}
How It Works
Host: www.example.org selects this block.
Exact names are the fastest match type.
Prefer listing www and apex when both are used.
Example 2 — Wildcard and Catch-all Style Names
*.example.org matches www and deeper labels. A lone _ is often used as a dummy name on a default server.
Nginx
server {
listen 80;
server_name *.example.org;
}
server {
listen 80 default_server;
server_name _;
return 444;
}
How It Works
*.example.org matches www.example.org and a.b.example.org.
_ is just an invalid hostname — not magic.
default_server on listen sets the fallback, not server_name.
Example 3 — Regex Server Name
Start the name with ~ and escape dots. Use anchors.