Links
- Server names
- Has explanation on matching rules.
- server_name in the wiki
- server_name in ngx_http_core,
Snippets
From Server names
When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:
- exact name
- longest wildcard name starting with an asterisk, e.g.
“
*.example.org
” - longest wildcard name ending with an asterisk, e.g.
“
mail.*
” - first matching regular expression (in order of appearance in a configuration file)
Using regular expressions and captures
To use regular expressions in server names,
prepend the name with a tilde "~
".
Named captures can be used in server_name
.
server { server_name ~^(?<subdomain>.+?)\.(?<domain>.+)$; root /sites/$domain/$subdomain; }
$hostname can be used as a server_name argument
server { server_name $hostname; }
Disabling requests that don't provide a "Host" header.
server { listen 80 default_server; # This is the default server name if not specified. server_name ""; return 444; # Just drop the requests. }