could not build server_names_hash: how to fix
Short answer
nginx -t failing with “could not build server_names_hash, you should increase server_names_hash_bucket_size” means a server_name is too long (or there are too many names) for the default hash bucket. Set server_names_hash_bucket_size 64 (then 128) in http {}, and raise server_names_hash_max_size for very many names.
Most common cause: Very long names (deep subdomains, long random prefixes).
Try this first: Raise it at http level
http {
server_names_hash_bucket_size 64; # use 128 if 64 is not enough
server_names_hash_max_size 1024; # for many names
}Last verified: · against the official docs listed below · Sources
Typical log lines
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
Common causes
- Very long names (deep subdomains, long random prefixes).
- Hundreds or thousands of server_name entries on one host.
- The default bucket size depends on the CPU cache line (32, 64 or 128), so a config can start failing on another machine.
Diagnosis commands
nginx -tnginx -T 2>/dev/null | grep -o 'server_name[^;]*' | awk '{for(i=2;i<=NF;i++) print length($i), $i}' | sort -rn | headFix configs
Raise it at http level
http {
server_names_hash_bucket_size 64; # use 128 if 64 is not enough
server_names_hash_max_size 1024; # for many names
}Run nginx -t first, then systemctl reload nginx.
Key facts and sources
- server_names_hash_bucket_size defaults to 32|64|128, depending on the processor's cache line size. [nginx.org]
- server_names_hash_max_size defaults to 512. [nginx.org]
- Official guidance: with many names, first set server_names_hash_max_size close to the number of names; for the bucket_size error, use the next power of two (e.g. 32 → 64). [nginx.org]
Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.
FAQ
Can it go inside server?
No. Both are http-level only; inside server nginx -t reports “directive is not allowed here”.
Do I need to act on the warn?
The warn does not block startup; lookups are just less efficient. Raise one of the two values to silence it.
Fix it with a tool
Related errors
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.