After a super-long domain name is configured for nginx, an error occurs when the configuration file is checked through/usr/local/nginx/sbin/ngnix-t:
| The code is as follows: |
Copy code |
| Cocould not build the server_names_hash, you shocould increase server_names_hash_bucket_size: 32 |
The solution is to add the following configuration in the http segment of the nginx configuration file:
| The code is as follows: |
Copy code |
| Server_names_hash_bucket_size 64; |
If it already exists, you need to increase the following value. Note: This value is a multiple of 32.
Nginx virtual host name on the official website
The virtual host name is defined using the server_name command and used to determine whether a virtual host can process requests. For more information, see how to handle a request using nginx. The virtual host name can be defined using the exact name, wildcard character, or regular expression:
| The code is as follows: |
Copy code |
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 nginx looks for a VM by name, if the name can match more than one host name definition, for example, if both the wildcard name and the regular expression name are matched, nginx looks for the VM by priority, select the first matching VM:
The exact name;
The longest wildcard name starting with a star: * .example.org;
The longest wildcard name ending with a star: mail .*;
The first matched regular expression name (in the order shown in the configuration file ).
Wildcard name
Wildcard names can only contain an asterisk at the beginning or end of a name, and are separated by dots. Therefore, both "www. * .example.org" and "w * .example.org" are invalid. However, the above two names can be described using a regular expression, that is, "~ ^ Www \... + \. example \. org $ "and" ~ ^ W. * \. example \. org $ ". The asterisks can match multiple sections with names (each section is separated by dots ). "* .Example.org" not only matches www.example.org, but also www.sub.example.org.
There is a special wildcard like ".example.org". It can match both the exact name "example.org" and the general wildcard name "* .example.org ".
Regular expression name
Nginx uses regular expressions that are compatible with PCRE. To use a regular expression, the virtual host name must be a tilde line "~" Start:
Server_name ~ ^ Www \ d + \. example \. net $;
Otherwise, the name is regarded as an exact name. If an expression contains an asterisk, it is considered a wildcard name (and possibly an invalid wildcard name ). Do not forget to set "^" and "$" anchors. They are not required in syntax, but logical. Note that the dot "." in the domain name must be escaped by the backslash. Regular expressions containing "{" and "}" must be referenced, for example:
Server_name "~ ^ (? <Name> \ w \ d {1, 3} +) \. example \. net $ ";
Otherwise, nginx cannot be started. The error message is:
Directive "server_name" is not terminated by ";" in...
The named regular expression capture group can be used as a variable later:
Server {
Server_name ~ ^ (Www \.)? (? <Domain>. +) $;
Location /{
Root/sites/$ domain;
}
}
PCRE uses the following syntax to support naming capture groups:
? <Name> support from PCRE-7.0, compatible with Perl 5.10 syntax
? 'Name' is supported from the PCRE-7.0 and is compatible with Perl 5.10 syntax
? P <name> supported from PCRE-4.0, compatible with Python syntax
If nginx cannot be started and an error message is displayed:
Pcre_compile () failed: unrecognized character after (? <In...
It indicates that the PCRE version is too old and should be used? P <name>. A capture group can also be referenced in numbers:
Server {
Server_name ~ ^ (Www \.)? (. +) $;
Location /{
Root/sites/$2;
}
}
However, this method is only applicable in simple cases (for example, the preceding example), because numeric references are easily overwritten.
Other types of names
Some host names are specially treated.
To use a non-default VM to process requests without the "Host" field in the request header, you must specify an empty name:
Server {
Listen 80;
Server_name example.org www.example.org "";
...
}
If server_name is not defined in the server block, nginx uses an empty name as the virtual host name.
Nginx 0.8.48 or earlier versions use the machine name as the virtual host name in the same case.
If the virtual host name is defined with "$ hostname" (nginx 0.9.4 or later), the machine name will be used.
If you use an IP address instead of a Host name to request a server, the "Host" field in the request header contains an IP address. The IP address can be used as the virtual host name to process such requests:
Server {
Listen 80;
Server_name nginx.org
Www.nginx.org
""
192.168.1.1
;
...
}
In the example of matching all servers, we can see a strange name "_":
Server {
Listen 80 default_server;
Server_name _;
Return 444;
}
This is nothing special. It is only one of thousands of illegal domain names that have no conflict with real names. Of course, you can also use "--" and "! @ # "And so on.
Nginx supports a special name "*" until version 0.6.25. This name is incorrectly interpreted as a match for all names. However, it never works as if it matches all the names or wildcards, but is used to support a function. This function is now supported by the server_name_in_redirect command. Therefore, the special name "*" is outdated. Replace it with the server_name_in_redirect command. Note that the server_name command cannot be used to describe matching all names or default servers. This is the attribute of the listen command, not the attribute of the server_name command. For more information, see how to handle a request using nginx. You can define two servers to listen to ports *: 80 and *: 8080, and then specify one as the default server with port *: 8080 and the other as the default server with port *: 80:
Server {
Listen 80;
Listen8080 default_server;
Server_name example.net;
...
}
Server {
Listen 80 default_server;
Listen 8080;
Server_name example.org;
...
}
Optimization
The exact name and wildcard name are stored in the hash table. Associate the hash table with the listening port. The size of the hash table is optimized in the configuration phase. You can find the name with the minimum CPU cache hit failure. For more information about how to set a hash table, see this document.
Nginx first searches for the hash table of the exact name. If no hash table is found, it searches for the hash table of the wildcard name starting with the star number. If it still does not exist, continue to search for the hash table of the wildcard name ending with the star number.
Because the names are searched by domain name section, it is slower to search for the hash table of the wildcard name than to search for the hash table of the exact name. Note that the special wildcard name ".example.org" is stored in the hash table of the wildcard name, but not in the hash table of the exact name.
Regular expressions are tested one by one in a serial mode, so they are the slowest and cannot be expanded.
For the above reasons, use the exact name as much as possible. For example, if example.org and www.example.org are used to access the server most frequently, it is more effective to clearly define them:
Server {
Listen 80;
Server_name example.org www.example.org * .example.org;
...
}
The following method is simpler but less efficient:
Server {
Listen 80;
Server_name .example.org;
...
}
If you have defined a large number of names or long names, you may need to use the server_names_hash_max_size and server_names_hash_bucket_size commands in the http configuration block to adjust them. The default value of server_names_hash_bucket_size may be 32, 64, or another value, depending on the length of the cache row of the CPU. If the value is 32, defining "too.long.server.name.example.org" as the virtual host name will fail, and nginx will display the following error message:
Cocould not build the server_names_hash,
You shoshould increase server_names_hash_bucket_size: 32
In this case, you need to double the value of the command:
Http {
Server_names_hash_bucket_size 64;
...
If a large number of names are defined, another error is returned:
Cocould not build the server_names_hash,
You shoshould increase either server_names_hash_max_size: 512
Or server_names_hash_bucket_size: 32
Set server_names_hash_max_size to the total number of names in the name list. If the problem persists, or the server starts slowly, increase the value of server_names_hash_bucket_size.
If a unique host is configured for only one listening port, nginx will not test the virtual host name at all (nor create a hash table for the listening port ). However, if the defined virtual host name is a regular expression containing a capture group, nginx will have to execute this expression to get the capture group.
Compatibility
V0.9.4 and later versions support special virtual hostnames "$ hostname ".
Starting from version 0.8.48, the default virtual host name is "".
Starting from version 0.8.25, the virtual host name can be captured using named regular expressions.
Starting from version 0.7.40, you can use regular expressions to capture groups in virtual host names.
Starting from version 0.7.12, a blank name "" is supported.
Starting from version 0.6.25, the wildcard and regular expression names can be used as the first virtual host name.
Starting from version 0.6.7, the virtual host name supports regular expressions.
Starting from version 0.6.0, wildcard names such as example. * are supported.
Starting from version 0.3.18, special wildcard names such as .example.org are supported.
Starting from version 0.1.13, wildcard names such as * .example.org are supported.