13

I have the following virtual host

server
{
  server_name abc.example.com;
  root /var/www/test/;
  index index.html;
}

When running nginx -s reload I get the following error:

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

Same happens for any server_name that has 15 or more characters.

If I set the server_name to ab.example.com (or any name under 15 characters) the problem stops manifesting.

To fix this I added the following to /etc/nginx/nginx.conf (it wasn't defined before):

server_names_hash_bucket_size 64;

Setting it to 33 worked as well, but not 32.

Why is the default maximum length 14 characters for server_name?

Is this limit imposed by nginx's default settings or by the system it runs on?

How does a server_name of 15 affect the maximum hash bucket size? (there are only 4 virtual hosts defined on the system)

Virgiliu
  • 231
  • 2
  • 7
  • What's the content of `grep "" /sys/devices/system/cpu/cpu?/cache/index?/coherency_line_size` ? – Xavier Lucas Jan 06 '15 at 18:48
  • @XavierLucas: `/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size:64` `/sys/devices/system/cpu/cpu0/cache/index1/coherency_line_size:64` `/sys/devices/system/cpu/cpu0/cache/index2/coherency_line_size:64` – Virgiliu Jan 07 '15 at 05:06

2 Answers2

11

This error occurs when the server_name is too large to fit in the hash bucket.

The default for server_names_hash_bucket_size is chosen depending on the CPU cache line size of the server. In particular it's meant to be as small as possible, to reduce CPU cache misses, as server_names must be looked up on every request.

As for why you're limited to 14 characters instead of the expected 31, I suspect one of two possibilities:

  • Your configuration file is in UTF-16 encoding (or some other encoding) instead of UTF-8, which causes nulls to appear before or after every character in the raw data, and doubling its size. This might happen if you edit the file on Windows. If this is the case, use something like iconv to fix it.
  • You've run into an unknown bug in nginx.
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Vim says that fileencoding=utf-8. I did a conversion using iconv to make sure that encoding is UTF8 but nothing changed in nginx behaviour. Maybe my VPS has a CPU with a very small cache line size, is there any way I check it and what would be a reasonable value for it? – Virgiliu Jan 06 '15 at 00:05
  • The reasonable value is the smallest power of two that nginx will start up successfully with. If you have a VPS it could be doing just about anything... – Michael Hampton Jan 06 '15 at 00:16
6

The default length should be automatically determined during the start, dependent on used server names, but this should also be updated during a reload operation. See if it works without manually setting the bucket size but with a restart instead of a reload.

See http://nginx.org/en/docs/hash.html to learn how hash sizes are determined and http://nginx.org/en/docs/http/server_names.html to read about hashes used for server names.

Sven
  • 98,649
  • 14
  • 180
  • 226