没有不值得去解决的问题,也没有不值得去学习的技术!

Resolve the “could not build optimal server_names_hash” Warning When Reloading Nginx 1.10

Reference URL: https://nginx.org/en/docs/http/server_names.html . Based on the error message, this should be caused by defining a large number of server names (the channel-pub-api-wx-auth.conf file is a newly added virtual host configuration file)

1. In Nginx version 1.10, an error occurs when reloading: nginx: [warn] could not build optimal server_names_hash, as shown in Figure 1.

In Nginx version 1.10, an error occurs when reloading: nginx: [warn] could not build optimal server_names_hash
Figure 1
Plaintext

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. View the Nginx configuration file, C:\nginx-1.10.1\conf\nginx.conf.

Nginx

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. View the virtual host configuration file, C:\nginx-1.10.1\conf\vhosts\channel-pub-api-wx-auth.conf.

Nginx

## 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 . Based on the error message, this should be caused by defining a large number of server names (the channel-pub-api-wx-auth.conf file is a newly added virtual host configuration file), as shown in Figure 2.

Reference URL: https://nginx.org/en/docs/http/server_names.html . Based on the error message, this should be caused by defining a large number of server names (the channel-pub-api-wx-auth.conf file is a newly added virtual host configuration file)
Figure 2
Plaintext

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 setting 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 becomes unacceptably long, try increasing server_names_hash_bucket_size. 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.

In this case, first try setting 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 becomes unacceptably long, try increasing server_names_hash_bucket_size. Edit the Nginx configuration file, C:\nginx-1.10.1\conf\nginx.conf, and add server_names_hash_max_size 1024
Figure 3
Nginx

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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系