emerg
could not build server_names_hash 的解决方法
快速答案
nginx -t 报 “could not build server_names_hash, you should increase server_names_hash_bucket_size” 是因为某个 server_name 太长(或域名太多),默认的哈希桶装不下。在 http {} 里把 server_names_hash_bucket_size 设为 64(仍不行就 128),域名特别多时再调大 server_names_hash_max_size。
最常见的原因:域名很长(多级子域名、长随机前缀)。
先这样改:在 http 层调大
http {
server_names_hash_bucket_size 64; # 不够再改 128
server_names_hash_max_size 1024; # 域名很多时
}最后核对: · 依据下方列出的官方文档 · 出处
典型日志
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64
常见原因
- 域名很长(多级子域名、长随机前缀)。
- 一台机器上有成百上千个 server_name。
- 桶大小默认取决于 CPU 缓存行(32、64 或 128),换了机器就可能突然报错。
排查命令
看报的是 emerg(必须改)还是 warn(建议改)
#
nginx -t最长的域名
#
nginx -T 2>/dev/null | grep -o 'server_name[^;]*' | awk '{for(i=2;i<=NF;i++) print length($i), $i}' | sort -rn | head修复配置
在 http 层调大
http {
server_names_hash_bucket_size 64; # 不够再改 128
server_names_hash_max_size 1024; # 域名很多时
}改完先 nginx -t,通过后 systemctl reload nginx。
关键事实与出处
- server_names_hash_bucket_size 默认 32|64|128,取决于处理器缓存行大小。 [nginx.org]
- server_names_hash_max_size 默认 512。 [nginx.org]
- 官方建议:域名很多时先把 server_names_hash_max_size 调到接近域名数量;遇到 bucket_size 报错时改成下一个 2 的幂(如 32 → 64)。 [nginx.org]
以上事实于 2026-11-14 对照原文核对;nginx 版本更新后请以官方文档为准。
常见问题
写在 server 里可以吗?
不行,这两个指令只能写在 http 层,写在 server 里 nginx -t 会报 “directive is not allowed here”。
warn 也要处理吗?
warn 不影响启动,只是查找效率稍差。按提示调大其中一个值即可消除。
用工具解决
相关报错
推广位(待接入)
这里以后会放 VPS / CDN 推荐,并标明「推广链接」。目前没有任何返佣链接。
这里以后会放 VPS / CDN 推荐,并标明「推广链接」。目前没有任何返佣链接。