Syntax: server_name name ...;
Default:
server_name "";
Context: server
Sets names of a virtual server, for example:
server {
server_name example.com www.example.com;
}
The first name becomes the primary server name.
Server names can include an asterisk (*
) replacing the first or last part of a name:
server {
server_name example.com *.example.com www.example.*;
}
Such names are called wildcard names.
The first two of the names mentioned above can be combined in one:
server {
server_name .example.com;
}
It is also possible to use regular expressions in server names, preceding the name with a tilde (~
):
server {
server_name www.example.com ~^www\d+\.example\.com$;
}
Regular expressions can contain captures (0.7.40) that can later be used in other directives:
server {
server_name ~^(www\.)?(.+)$;
location / {
root /sites/$2;
}
}
server {
server_name _;
location / {
root /sites/default;
}
}
Named captures in regular expressions create variables (0.8.25) that can later be used in other directives:
server {
server_name ~^(www\.)?(?<domain>.+)$;
location / {
root /sites/$domain;
}
}
server {
server_name _;
location / {
root /sites/default;
}
}
If the directive’s parameter is set to $hostname
(0.9.4), the machine’s hostname is inserted.
It is also possible to specify an empty server name (0.7.11):
server {
server_name www.example.com "";
}
It allows this server to process requests without the Host
header field — instead of the default server — for the given address:port pair. This is the default setting.
During searching for a virtual server by name, if the name matches more than one of the specified variants, (e.g. both a wildcard name and regular expression match), the first matching variant will be chosen, in the following order of priority:
- the exact name
- the longest wildcard name starting with an asterisk, e.g.
*.example.com
- the longest wildcard name ending with an asterisk, e.g.
mail.*
- the first matching regular expression (in order of appearance in the configuration file)