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

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

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


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


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+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理