在 Nginx 1.10 版本中,重新加载时报错:nginx: [warn] could not build optimal server_names_hash 的分析解决

1、在 Nginx 1.10 版本中,重新加载时报错:nginx: [warn] could not build optimal server_names_hash,如图1

图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、查看 Nginx 配置文件,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、查看虚拟主机配置文件,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、参考网址:https://nginx.org/en/docs/http/server_names.html ,根据错误提示信息,应该是由于定义了大量服务器名称导致的(channel-pub-api-wx-auth.conf 文件为新增加的虚拟主机配置文件),如图2

图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、在这种情况下,首先尝试将 server_names_hash_max_size 设置为接近服务器名称数的数字。只有当这没有帮助时,或者如果 nginx 的开始时间长得令人无法接受,请尝试增加 server_names_hash_bucket_size。编辑 Nginx 配置文件,C:\nginx-1.10.1\conf\nginx.conf,添加 server_names_hash_max_size 1024。如图3

图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;
}

 

永夜