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

Solution 1:

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.

Solution 2:

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.