Why is 14 characters the maximum length for the nginx server_name directive?

yoshi asked:

I have the following virtual host

  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)

My answer:

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.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.