Directive can have multiple meanings. Each variant is separated with horizontal line


[edit] server_name

Syntax: server_name name ...
Default: ""
Context: server
Reference: server_name


This directive performs two actions:

  1. full, static names
  2. names with a wildcard at the start of the name — *.example.com
  3. names with a wildcard at the end of the name — www.example.*
  4. names with regular expressions
If there is no match, a server { ... } block in the configuration file will be used based on the following order:
  1. the server block with a matching listen directive marked as [default|default_server]
  2. the first server block with a matching listen directive (or implicit listen 80;)

Example:

server {
  server_name   example.com  www.example.com;
}

The first name becomes the basic name of server. By default the name of the machine (hostname) is used.

It is possible to use "*" for replacing the first or the last part of the name:

server {
  server_name   example.com  *.example.com  www.example.*;
}

The first two of the above names (example.com and *.example.com) can be combined into one:

server {
  server_name  .example.com;
}

It is also possible to use regular expressions in server names, prepending the name with a tilde "~" like so:

server {
  server_name   www.example.com   ~^www\d+\.example\.com$;
}

Since nginx 0.7.12, an empty server name is supported to catch the requests without "Host" header, please note that most browsers will always send a Host header, if accessed by IP the Host header will contain the IP. To specify a catch-all block please see the default_server flag of the listen directive.

server {
  server_name "";
}

Since nginx 0.8.25 named captures can be used in server_name:

server {
  server_name   ~^(www\.)?(?<domain>.+)$;
  root  /sites/$domain;
}

and multiple name captures:

server {
  server_name   ~^(?<subdomain>.+?)\.(?<domain>.+)$;
  root  /sites/$domain/$subdomain;
}

Some older versions of PCRE may have issues with this syntax. If any problems arise try this following syntax:

server {
  server_name   ~^(www\.)?(?P<domain>.+)$;
  root  /sites/$domain;
}

Since nginx 0.9.4, $hostname can be used as a server_name argument:

server {
  server_name $hostname;
}

Module: HttpCoreModule

[edit] server_name

syntax: server_name name fqdn_server_host

default: The name of the host, obtained through gethostname()

context: mail, server

Directive assigns the names of virtual server, for example:

server {
  server_name   example.com  www.example.com;
}

The first name becomes the basic name of server. By default the name of the machine (hostname) is used. It is possible to use "*" for replacing the first part of the name:

server {
  server_name   example.com  *.example.com;
}

Two of the given name of the above example can be combined into one:

server {
  server_name  .example.com;
}

The basic name of server is used in an HTTP redirects, if no a "Host" header was in client request or that header does not match any assigned server_name. You can also use just "*" to force Nginx to use the "Host" header in the HTTP redirect (note that "*" cannot be used as the first name, but you can use a dummy name such as "_" instead):

server {
  server_name example.com *;
}
server {
  server_name _ *;
}


Module: MailCoreModule