server_name 

Send to Kindle
home » snippets » nginx » server_name



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:

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.
}