The server_name
directive in Nginx is used to define which server block (virtual host) should process incoming requests based on the Host
HTTP header. It allows hosting multiple domains or subdomains on a single server. Here's a detailed breakdown:
Syntax
server {
listen 80;
server_name example.com www.example.com;
...
}
Key Features
-
Exact Matches
List domain names explicitly:server_name example.com www.example.com;
- Handles requests for
example.com
andwww.example.com
.
- Handles requests for
-
Wildcard Patterns
Use*
to match subdomains or domain prefixes:server_name *.example.com; # Matches blog.example.com, shop.example.com, etc. server_name example.*; # Matches example.com, example.net, etc.
- Wildcards work only at the start or end of a domain segment (not in the middle).
-
Regular Expressions
Prefix patterns with~
for complex matching:server_name ~^www\d+\.example\.com$; # Matches www1.example.com, www2.example.com, etc. server_name ~^(?<subdomain>.+)\.example\.com$; # Captures subdomains into a variable.
- Use
^
and$
to anchor matches. - Escape dots (
.
) with a backslash (\.
).
- Use
-
Default Server
Specify a fallback server block when noHost
header matches:listen 80 default_server; server_name _; # Catch-all (deprecated in some contexts; prefer `default_server` in `listen`).
Priority Order
Nginx resolves conflicts using this priority:
- Exact name (e.g.,
example.com
). - *Longest wildcard starting with `
** (e.g.,
*.example.com`). - *Longest wildcard ending with `
** (e.g.,
mail.*`). - First matching regular expression (in configuration file order).
Examples
# 1. Handle main domain + www
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
}
# 2. Catch all subdomains of example.com
server {
listen 80;
server_name *.example.com;
root /var/www/subdomains;
}
# 3. Regex to match numeric subdomains (e.g., shop123.example.com)
server {
listen 80;
server_name ~^shop\d+\.example\.com$;
root /var/www/shops;
}
# 4. Default server for unmatched requests
server {
listen 80 default_server;
server_name _;
return 444; # Close connection (Nginx-specific non-standard code)
}
Best Practices
-
Set
default_server
Explicitly
Avoid relying on the first server block as the default. Specify it in thelisten
directive:listen 80 default_server;
-
Use Regex Sparingly
Wildcards are more efficient than regular expressions. -
Test Configurations
Usenginx -t
to check syntax andnginx -s reload
to apply changes. -
Handle
www
and Non-www
Redirect betweenwww
and non-www
versions for consistency:server { server_name example.com; return 301 https://www.example.com$request_uri; }
Common Pitfalls
- Overlapping Patterns: Ensure no conflicting
server_name
rules. - Case Sensitivity: Domains are matched case-insensitively (e.g.,
EXAMPLE.COM
works). - Missing
Host
Header: Requests without aHost
header (e.g., HTTP/1.0) go to the default server.
By mastering server_name
, you can efficiently manage multiple domains and subdomains on a single Nginx instance.