Server names | ![]() english עברית русский türkçe news about download security advisories documentation introduction howto faq wiki links books support | |||||||
Server names are defined using the The names are tested in the following order:
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 names
If no If 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 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
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 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
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 Compatibility
|