Server names | english русский 简体中文 עברית 日本語 türkçe news about download security advisories documentation pgp keys faq links books support donation trac wiki nginx.com | |||||||
Server names are defined using the server_name directive and determine which server block is used for a given request. See also “How nginx processes a request”. They may be defined using exact names, wildcard names, or regular expressions: server { listen 80; server_name example.org www.example.org; ... } server { listen 80; server_name *.example.org; ... } server { listen 80; server_name mail.*; ... } server { listen 80; server_name ~^(?<user>.+)\.example\.net$; ... }
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:
Wildcard names
A wildcard name may contain an asterisk only on the name’s start or end,
and only on a dot border. The names “
A special wildcard name in the form “ Regular expressions namesThe regular expressions used by nginx are compatible with those used by the Perl programming language (PCRE). To use a regular expression, the server name must start with the tilde character: server_name ~^www\d+\.example\.net$;
otherwise it will be treated as an exact name, or if the expression contains
an asterisk, as a wildcard name (and most likely as an invalid one).
Do not forget to set “ server_name "~^(?<name>\w\d{1,3}+)\.example\.net$"; otherwise nginx will fail to start and display the error message: directive "server_name" is not terminated by ";" in ... A named regular expression capture can be used later as a variable: server { server_name ~^(www\.)?(?<domain>.+)$; location / { root /sites/$domain; } } The PCRE library supports named captures using the following syntax: If nginx fails to start and displays the error message: pcre_compile() failed: unrecognized character after (?< in ...
this means that the PCRE library is old and the syntax
“ server { server_name ~^(www\.)?(.+)$; location / { root /sites/$2; } } However, such usage should be limited to simple cases (like the above), since the digital references can easily be overwritten. Miscellaneous namesThere are some server names that are treated specially. If it is required to process requests without the “Host” header field in a server block which is not the default, an empty name should be specified: server { listen 80; server_name example.org www.example.org ""; ... }
If no server_name is defined in a server block then nginx uses the empty name as the server name. nginx versions up to 0.8.48 used the machine’s hostname as the server name in this case.
If a server name is defined as “ If someone makes a request using an IP address instead of a server name, the “Host” request header field will contain the IP address and the request can be handled using the IP address as the server name: server { listen 80; server_name example.org www.example.org "" 192.168.1.1 ; ... }
In catch-all server examples the strange name “ server { listen 80 default_server; server_name _; return 444; }
There is nothing special about this name, it is just one of a myriad
of invalid domain names which never intersect with any real name.
Other invalid names like “
nginx versions up to 0.6.25 supported the special name “ server { listen 80; listen 8080 default_server; server_name example.net; ... } server { listen 80 default_server; listen 8080; server_name example.org; ... }
OptimizationExact names, wildcard names starting with an asterisk, and wildcard names ending with an asterisk are stored in three hash tables bound to the listen ports. The sizes of hash tables are optimized at the configuration phase so that a name can be found with the fewest CPU cache misses. Details of setting up hash tables are provided in a separate document. The exact names hash table is searched first. If a name is not found, the hash table with wildcard names starting with an asterisk is searched. If the name is not found there, the hash table with wildcard names ending with an asterisk is searched.
Searching wildcard names hash table is slower than searching exact names hash
table because names are searched by domain parts.
Note that the special wildcard form “ Regular expressions are tested sequentially and therefore are the slowest method and are non-scalable.
For these reasons, it is better to use exact names where possible.
For example, if the most frequently requested names of a server
are server { listen 80; server_name example.org www.example.org *.example.org; ... } than to use the simplified form: server { listen 80; server_name .example.org; ... }
If a large number of server names are defined,
or unusually long server names are defined, tuning
the server_names_hash_max_size
and server_names_hash_bucket_size
directives at the http level may become necessary.
The default value of the
server_names_hash_bucket_size
directive may be equal to 32, or 64, or another value,
depending on CPU cache line size.
If the default value is 32 and server name is defined as
“ could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 In this case, the directive value should be increased to the next power of two: http { server_names_hash_bucket_size 64; ... If a large number of server names are defined, another error message will appear: could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 32 In such a case, first try to set server_names_hash_max_size to a number close to the number of server names. Only if this does not help, or if nginx’s start time is unacceptably long, try to increase server_names_hash_bucket_size. If a server is the only server for a listen port, then nginx will not test server names at all (and will not build the hash tables for the listen port). However, there is one exception. If a server name is a regular expression with captures, then nginx has to execute the expression to get the captures. Compatibility
|