Nginx could not build the server_names_hash, you should increase server_names_hash_bucket_size

时间:2019-08-20 17:58:27  来源:igfitidea点击:

问题

nginx服务无法启动:

[root@server ~]# systemctl restart nginx
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
[root@server ~]#

根据提示,运行命令“journalctl-xn”。

[root@server nginx]# journalctl -xn
— Logs begin at Thu 2014-11-27 04:58:50 EST, end at Sat 2014-11-29 00:01:19 EST. —
Nov 29 00:01:01 theitroad.local anacron[14989]: Normal exit (0 jobs run)
Nov 29 00:01:01 theitroad.local run-parts(/etc/cron.hourly)[14991]: finished 0anacron
Nov 29 00:01:01 theitroad.local run-parts(/etc/cron.hourly)[14993]: starting 0yum-hourly.cron
Nov 29 00:01:01 theitroad.local run-parts(/etc/cron.hourly)[14997]: finished 0yum-hourly.cron
Nov 29 00:01:19 theitroad.local systemd[1]: Starting The nginx HTTP and reverse proxy server…
— Subject: Unit nginx.service has begun with start-up
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
—
— Unit nginx.service has begun starting up.

Nov 29 00:01:19 theitroad.local nginx[15001]: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size


Nov 29 00:01:19 theitroad.local nginx[15001]: nginx: configuration file /etc/nginx/nginx.conf test failed
Nov 29 00:01:19 theitroad.local systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 29 00:01:19 theitroad.local systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
— Subject: Unit nginx.service has failed
— Defined-By: systemd
— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
—
— Unit nginx.service has failed.
—
— The result is failed.
Nov 29 00:01:19 theitroad.local systemd[1]: Unit nginx.service entered failed state.
[root@server nginx]#

解决方案

原因

如果定义的服务器名过长,可能就需要在http级别调优server_names_hash_max_size和server_names_hash_bucket_size指令。

server_names_hash_bucket_size指令的默认值可能等于32、64或其他值,具体取决于CPU缓存线的大小。
如果默认值是32,我们定义一个服务器名为“very-long-server.mydomain.example.org”,显然太长了,超过了32个字符,那么nginx将无法启动并显示错误。

要解决此问题,在nginx.conf添加server_names_hash_bucket_size设置

http {
..
server_names_hash_bucket_size 64;
..
}

如果问题没有解决,那么改成128。

检查nginx配置:

nginx -t

如果一切正常,重新启动nginx服务。

在CentOS 7/RHEL 7上

systemctl restart nginx.service

在CentOS–6/5/RHEL–6/5/Debian/Ubuntu上

sudo service nginx restart