A Configuration Guide
[ 32 ]
Besides a normal string, NGINX will accept a wildcard as a parameter to the
server_name directive:
- The wildcard can replace the subdomain part: *.example.com
- The wildcard can replace the top-level-domain part: http://www.example.*
- A special form will match the subdomain or the domain itself:
.example.com (matches *.example.com as well as example.com)
A regular expression can also be used as a parameter to server_name by prepending
the name with a tilde (~):
server_name ~^www.example.com$;
server_name ~^www(\d+).example.(com)$;
The latter form is an example using captures, which can later be referenced
(as $1, $2, and so on) in further configuration directives.
NGINX uses the following logic when determining which virtual server should
serve a specific request:
- Match the IP address and port to the listen directive.
- Match the Host header field against the server_name directive as a string.
- Match the Host header field against the server_name directive with a
wildcard at the beginning of the string. - Match the Host header field against the server_name directive with a
wildcard at the end of the string. - Match the Host header field against the server_name directive as a regular
expression. - If all the Host headers match fail, then direct to the listen directive
marked as default_server. - If all the Host headers match fail and there is no default_server,
direct to the first server with a listen directive that satisfies step 1.