In nginx 1.10 version, an error is reported when reloading: nginx:[warn]Could not build optimal server_names_hash
1. In nginx 1.10 version, an error is reported when reloading: nginx:[warn]Could not build optimal server_names_hash, as shown in Figure 1
PS C:\nginx-1.10.1> ./nginx -s reload
nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or s
erver_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
2. Check the nginx configuration file, C:\nginx-1.10.1\conf\nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server_names_hash_bucket_size 64;
}
3. Check the virtual host configuration file, C:\nginx-1.10.1\conf\vhosts\channel-pub-api-wx-auth.conf
## FRONTEND ##
server {
charset utf-8;
client_max_body_size 200m;
client_body_buffer_size 1024k;
fastcgi_read_timeout 180s;
listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
server_name www.channel-pub-api-wx-auth.localhost www.channel-pub-api-wx-auth-localhost.chinamcloud.cn;
root E:/wwwroot/channel-pub-api-wx-auth/frontend/web;
index index.php;
}
## API ##
server {
charset utf-8;
client_max_body_size 200m;
client_body_buffer_size 1024k;
fastcgi_read_timeout 180s;
listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
server_name api.channel-pub-api-wx-auth.localhost api.channel-pub-api-wx-auth-localhost.chinamcloud.cn;
root E:/wwwroot/channel-pub-api-wx-auth;
index index.php;
}
4. Reference URL:https://nginx.org/en/docs/http/server_names.html, according to the error message, it should be caused by the definition of a large number of server names (channel-pub-api-wx-auth.conf file is the newly added virtual host configuration file), as shown in Figure 2
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 “too.long.server.name.example.org” 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, 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.
5. In this case, first try to set server_names_hash_max_size to a number that is close to the number of server names. Try adding SERVER_NAMES_HASH_BUCKET_SIZE only if this doesn’t help, or if the start time of nginx is unacceptable. Edit the Nginx configuration file, C:\nginx-1.10.1\conf\nginx.conf, and add server_names_hash_max_size 1024. as shown in Figure 3
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server_names_hash_max_size 1024;
server_names_hash_bucket_size 64;
}
![在 Nginx 1.10 版本中,重新加载时报错:nginx: [warn] could not build optimal server_names_hash](https://www.shuijingwanwq.com/wp-content/uploads/2019/06/1-3.png)

