Ffile2fix
Sign in Get started

How to fix Nginx "could not build server_names_hash"

nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64

Nginx stores all server names in a hash table, and one of your names is too long for the default bucket size, or there are too many names. Nginx refuses to start or reload until you raise the size. It often appears after adding a long subdomain or many domains.

Also appears as: nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32 · nginx: [emerg] could not build server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64 · nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size

Common causes

  • A long domain name, such as a long subdomain or cloud hostname
  • Many server names across all server blocks
  • server_names_hash_bucket_size left at its default, which depends on CPU cache line size (32 or 64)
  • The directive was added inside a server block instead of the http block
  • Duplicate or wildcard-heavy server_name lists

How to fix it

  1. Raise the bucket size. In /etc/nginx/nginx.conf, inside the http { } block, add server_names_hash_bucket_size 128;. On Ubuntu there is often a commented line you can uncomment and edit.
  2. Raise max size if mentioned. If the error also names server_names_hash_max_size, add server_names_hash_max_size 1024; (or 2048 for very many names) in the http block.
  3. Place it in the http context. These directives are only valid in http { }. Putting them in a server block causes a different error.
  4. Test the config. Run sudo nginx -t. If it still fails, double the value again (bucket size should stay a power of two, like 128 or 256).
  5. Reload Nginx. Run sudo systemctl reload nginx. Check that all sites respond.

Increase hash sizes in /etc/nginx/nginx.conf

http {
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 1024;
    # ...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

How to stop it happening again

  • Set the hash sizes in your base Nginx template
  • Run nginx -t before reloading on every domain addition
  • Remove old domains you no longer serve
  • Avoid duplicate names across server blocks

File2fix tools for this error

Frequently asked questions

Does a bigger bucket size slow Nginx down?

Not noticeably. The hash is built once at startup and lookups stay fast.

What value should I use?

128 fixes most cases. Use 256 only if you have very long names; keep it a power of two.

Why did it appear after adding one domain?

That domain is longer than the current bucket allows. It only takes one long name to trigger the error.