Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

nginx: could not build the server_names_hash, you should increase server_names_hash_bucket_size

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

If nginx will not start, or a reload is refused, with could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32, a server name is too long for the hash bucket nginx is using. Raise server_names_hash_bucket_size in the http block to the next power of two. If the message instead says "either server_names_hash_max_size" "or server_names_hash_bucket_size", you have many names, and server_names_hash_max_size is the one to raise first.

What the error means

nginx stores server names in hash tables so that it can match the Host header of a request quickly. The nginx page "Setting up hashes" says that "During the start and each re-configuration nginx selects the minimum possible sizes of hash tables such that the bucket size that stores keys with identical hash values does not exceed the configured parameter (hash bucket size)", and that the adjustment continues "until the table size exceeds the hash max size parameter". The two messages below ask you to raise one of those two limits.

The nginx page "Server names" gives both messages. For a name longer than the bucket can hold, with a default bucket size of 32, "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

The number at the end is the bucket size in use. The page says "the directive value should be increased to the next power of two", and shows server_names_hash_bucket_size 64; in the http block.

For a large number of names, "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

For that message the page says: "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."

The directive pages give the defaults and the context. server_names_hash_bucket_size has the default 32|64|128 ("The default value depends on the size of the processor’s cache line") and server_names_hash_max_size defaults to 512. Both are valid in the http context only.

The hash page adds a general rule: "if nginx emits the message requesting to increase either hash max size or hash bucket size then the first parameter should first be increased." It also says the bucket size "is aligned to the size that is a multiple of the processor’s cache line size", and the default 32|64|128 depends on the cache line size.

The Beginner's Guide explains what a failed reload does. On nginx -s reload the master process "checks the syntax validity of the new configuration file and tries to apply the configuration provided in it", and if that fails, "the master process rolls back the changes and continues to work with the old configuration." On a cold start there is no old configuration, so nginx does not start.

Likely causes

Cause What you see How to confirm
One unusually long server_name The first message form, ending in the current bucket size The server_name lines list a name longer than the others, near or above the bucket size in the message
Many server_name entries The second message form, naming both directives The count of names in the configuration is in the hundreds or more
Both a long name and many names The second form again after you raised server_names_hash_max_size The second message still appears after that change, or nginx starts slowly
The directive sits in the wrong block The change has no effect, or the test reports a directive error The directive is inside server or location and not http
A new site was added by an include and the list grew The error began right after a new file was added Compare the names before and after (step 3 lists them)

Prove the cause before changing anything

All of these only read the configuration. Run them as root.

1. Test the configuration and read the message.

nginx -t

The command-line page says -t tests "the configuration file: nginx checks the configuration for correct syntax, and then tries to open files referred in the configuration." The message printed tells you which of the two forms you have and the current bucket size.

2. Read the configuration files directly.

The next steps read the files on disk, not a dump from nginx -T. The command-line page says -T is the same as -t but "additionally dump configuration files to standard output"; in src/core/nginx.c (nginx tag release-1.31.10) the dump comes after the configuration was accepted, so when the test fails, as it does here, nginx -T prints no dump. Use the directory that holds your configuration; /etc/nginx is the usual one.

3. List the server_name lines and look for unusually long names.

grep -Rn -E '^\s*server_name\s' /etc/nginx/

The command lists every server_name line, with its file name and line number. Read the list for the longest names. The nginx page gives no safe length: its own failing example, too.long.server.name.example.org, is 32 characters long and fails with a bucket size of 32, so a name of exactly the bucket size can already fail. Each server_name line can carry several names, so a count of lines is smaller than a count of names.

4. Find where the hash directives are already set.

grep -Rn 'server_names_hash' /etc/nginx/

A good result for a fix is no match (the defaults are in use) or a match inside http. A match inside a server block means the value is in a context where the directive is not valid.

The server names page also says: "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)." So the message needs at least two servers sharing a listen port, or the exception the page names for a regular expression with captures.

Fix

The directive depends on the message read in step 1.

  • The first message (... increase server_names_hash_bucket_size: 32) calls for server_names_hash_bucket_size at the next power of two above the number in the message (32 becomes 64, 64 becomes 128).
  • The second message (either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 32) calls for server_names_hash_max_size at a number close to the count of server names, with the bucket size raised only if that does not help.

First save the current file. The example uses the main file; use the file that holds your http block:

cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.2026-12-01

Disruptive step: the next edit changes a setting that applies to every server in the http block, and the reload that follows starts new worker processes and asks the old ones to finish and exit. If the new file fails the test, nothing is reloaded.

Edit the http block of /etc/nginx/nginx.conf (the file saved above) and add one line. For the first message:

http {
    server_names_hash_bucket_size 64;
    # ... the rest of the existing http block unchanged
}

For the second message, add server_names_hash_max_size with your own figure instead (for example server_names_hash_max_size 1200; for about 1,150 names). Then test, and reload only if the test reports no error:

nginx -t
nginx -s reload

If nginx was not running because the start failed, the reload signal has nothing to reach. Start it with your service manager after the test passes.

To undo it: restore the file saved above under its original name, test, and reload.

ls -l /etc/nginx/nginx.conf.2026-12-01 && cp -p /etc/nginx/nginx.conf.2026-12-01 /etc/nginx/nginx.conf
nginx -t
nginx -s reload

Example (illustrative): web01 serves several sites from /etc/nginx/conf.d/. A new file adds a site named reporting-portal-branch-offices.example.com, which is 43 characters, more than the bucket size of 32 and less than 64. nginx -t fails with the first message form ending in : 32. Step 3 lists the server_name lines, and the new name is the longest entry in the list. The engineer saves nginx.conf, adds server_names_hash_bucket_size 64; to the http block (the next power of two above 32), and nginx -t now reports no error. After nginx -s reload, requests for the old and the new site name reach the right server.

Confirm the fix

  1. nginx -t reports no error and no server_names_hash message.
  2. After the reload, request each site name that matters, for example with curl -I https://app.example.com/, and check that each is answered by the right server. The server names page says the match is made in a fixed order (exact names first, then wildcards, then regular expressions), so a wrong server points to a name or order problem, not to the hash.
  3. On a server that did not start, the service stays up after the start command.

What to watch afterwards

  • Run nginx -t before every reload that adds a site. A long or numerous names list is the usual trigger, and the test finds it before visitors do.
  • The hash page says that when the bucket size equals one cache line, a key search takes two memory accesses in the worst case. It gives no figure for larger buckets, so keep the value only as large as it needs to be.
  • If the site count keeps growing, record the figure you chose for server_names_hash_max_size next to the count it was chosen for.
  • For a certificate or site that answers with the wrong content, see "Browser shows the wrong certificate for a site: SNI and the default virtual host".

Risks and limits

  • These commands have not been run or lab-tested.
  • The nginx documentation says a size "close to the number of server names" and "the next power of two". It gives no formula for the exact bucket size a given name needs, so the steps raise the value and test, and repeat if the message returns.
  • The nginx documentation is the current one on the site and shows no version for these directives. Check the page for the version you run.
  • The nginx documentation gives no name length that is safe for a given bucket size, so the steps list the names and compare them with the message.

Sources