Server names | ![]() english עברית 日本語 русский türkçe news about download security advisories documentation introduction pgp keys howto faq trac wiki links books support donation 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: The names are tested in the following order:
The first match stops the search. 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 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+\.nginx\.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 “^” and “$” anchors. They are not required syntactically, but logically. Also note that domain name dots should be escaped with a backslash. A regular expression containing the characters “{” and “}” should be quoted: 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: 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 you should try the syntax “ However, such usage should be limited to simple cases (like the above), since the digital references can easily be overwritten. Miscellaneous namesIf you want to process requests without a “Host” header line in a server block which is not the default, you should specify an empty name:
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 hostname as the server name in this case.
If someone makes a request using an IP address instead of a server name, the request’s “Host” header line will contain the IP address and you can handle the request using the IP address as the server name:
In catch-all server examples you may see the strange name “_”: 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. You may also use something like “--”, “!@#”, and so on. nginx versions up to 0.6.25 supported the special name “*” which was erroneously interpreted to be a catch-all name. It never functioned as a catch-all or wildcard server name. Instead, it supplied the functionality that is now provided by the server_name_in_redirect directive. The special name “*” is now deprecated and the server_name_in_redirect directive should be used. Note that there is no way to specify the catch-all name or the default server using the server_name directive. This is a property of the listen directive and not of the server_name directive. See also “How nginx processes a request”. You can define servers listening on ports *:80 and *:8080, and direct that one will be the default server for port *:8080, while the other will be the default for port *:80:
Optimization
Exact names and wildcard names are stored in hashes.
The hashes are bound to the listen ports and every listen port
may have up to three hashes: an exact names hash, a wildcard names hash
starting with an asterisk, and a wildcard names hash ending with an asterisk.
The sizes of the hashes are optimized at the configuration phase so that
a name can be found with the fewest CPU cache misses.
The exact names hash is searched first.
If a name is not found using the exact name hash, then the wildcard names hash
starting with an asterisk is searched.
If the name is not found there, the wildcard names hash
ending with an asterisk is searched.
Searching wildcard names hashes is slower than searching exact name hash
because names are searched by domain parts.
Note that the special wildcard form “ For these reasons, it is better to use exact names where possible. For example, if the most frequently requested names of a server are nginx.org and www.nginx.org, it is more efficient to define them explicitly: than to use the simplified form:
If you have defined a large number of server names, or defined unusually long server names, you may need to tune the server_names_hash_max_size and server_names_hash_bucket_size directives at the http level. The default value of the server_names_hash_bucket_size may be equal to 32, or 64, or another value, depending on your CPU cache line size. If the default value is 32 and you define “too.long.server.name.nginx.org” as a server name, then nginx will fail to start and display the error message: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 In this case, you should set the directive value to the next power of 2: If you have defined a large number of server names, you will get another error message: could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 32 You should 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, should you 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 hashes 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
|