Fix nginx "could not build server_names_hash" (bucket_size)
nginx "could not build server_names_hash" means one server_name is too long for the bucket. Set server_names_hash_bucket_size 128 once in http.
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64nginx: configuration file /etc/nginx/nginx.conf test failedOne of your server_name values is longer than a hash bucket, so nginx refuses to start. Add server_names_hash_bucket_size 128; to the http block once and it starts. The trailing 64 is the current value; the official docs say to raise it to the next power of two.
[1] Read the message to tell the two cases apart. A long name and many names produce different messages.
| Message | Level | Cause | Raise first |
|---|---|---|---|
could not build server_names_hash, you should increase server_names_hash_bucket_size: 64 | [emerg], start fails | one long name | server_names_hash_bucket_size |
could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size | [warn], still starts | a very large number of names | server_names_hash_max_size |
The official docs (Server names, Optimization) call the second one "another error message", but with 3,000 names on 1.30.6 it was a [warn] and nginx -t passed. The docs also print could not build the server_names_hash with a the that the real output does not have, so search for the exact text above.
[2] Find the longest name. While the error is active, nginx -T prints nothing (zero lines when the test fails), so read the files directly.
$ sudo nginx -T 2>/dev/null | wc -l$ sudo grep -RhE '^\s*server_name\s' /etc/nginx/nginx.conf /etc/nginx/conf.d/ /etc/nginx/sites-enabled/ 2>/dev/null | tr -s ' \t;' '\n' | grep -vE '^(server_name)?$' | awk '{ print length, $0 }' | sort -rn | head -n 3055 preview-feature-branch-1234.staging.eu-west.example.com19 www.app.example.com15 app.example.comUse **-R**, not -r. On Ubuntu, entries in sites-enabled/ are symlinks to sites-available/, and grep -r does not follow symlinks inside a directory. In the same Ubuntu test, -r skipped the 55-character name of the symlinked site and put 19 www.app.example.com on top. The 2>/dev/null hides No such file or directory where sites-enabled/ does not exist, as in the official nginx image and nginx.org packages. If you include other paths, add them too.
In this test (64-bit, default 64) names up to 46 characters passed and 47 characters triggered the error. Bucket bookkeeping is added to each name; the exact formula was not confirmed in the official docs. Long preview or branch subdomains are a common trigger.
[3] Set the value in the http block. The Ubuntu package ships the line commented out in /etc/nginx/nginx.conf:
23: # server_names_hash_bucket_size 64;Uncomment it and change it to 128. Leaving it at 64 changes nothing, because 64 is already the default here.
http { server_names_hash_bucket_size 128; ...}Files in conf.d/*.conf are read inside http, so you can put it there instead — but not in both places. Setting the directive twice gives a different error (on Ubuntu, uncommenting it in nginx.conf and adding it in conf.d produced the same directive is duplicate):
nginx: [emerg] "server_names_hash_bucket_size" directive is duplicate in /etc/nginx/conf.d/default.conf:2nginx: configuration file /etc/nginx/nginx.conf test failedKeep one. If 128 is still not enough, use 256.
[4] For many names, raise server_names_hash_max_size first. The docs say to try a value close to the number of names, and to raise the bucket size only if that fails or startup gets too slow. With 3,000 names, 4096 still warned and 8192 was clean.
http { server_names_hash_max_size 8192; ...}nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 4096 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_sizeThat is the warning at 4096. The number in the message is the current value, so double it until the warning is gone.
[5] Test and reload.
$ sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulIf the test fails, the reload does not run and the running nginx keeps its old config. To roll back, delete or re-comment the line and run the same command.
For the warning about the same name in two blocks see conflicting server name … ignored; for port errors see bind() Address already in use.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.