NGINX Error: could not build the server_names_hash

server: NGINX

could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64

The Problem

As my server grows, so do the number of server names. You may ask yourself, how many server names could one person have?! www.teition.com and teition.com is only two!

NGINX has a scaleable feature known as Server Blocks (Apache uses the name Virtual Host) (see NGINX examples of server blocks here). These allow a single server to serve more than one domain name. In this way, a single server can have one IP and multiple domain names. I won’t get into the specifics of a deployment process, but rest assured, there are benefits and naturally some drawbacks to doing this.

So, I went to add two server names to a server block under the server context, greatly simplified:

server {
server_name servername.com newserver1.com newserver2.com;
}

Simple enough change, I’ve done it to many blocks before. Of course, after doing this, one must restart NGINX in the console:

sudo service nginx restart

which promptly [fail]ed.

I checked the NGINX server error logs, by default located in \var\log\nginx\error.log and found:

could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64

The Solution

Reading the above error message, seems pretty easy to understand one must increase the size of one of the two. According to NGINX hash documentation, whichever element shows up first in the error log, it is suggested to increase that one.

Thus, for me, this meant changing the server_names_hash_max_size directive. This is located inside the http context, the \etc\nginx\nginx.conf file.

However, when I went to look at the nginx.conf file, there was no existing line: server_names_hash_max_size, but I did find the line: server_names_hash_bucket_size 64; (which was octothorp’d out meaning it was set to default according to the NGINX hash documentation).

I then, just above the ‘server_names_hash_bucket_size 64;’ line, added:

server_names_hash_max_size 700;

Making it just slightly larger then the default size of 512.

side note: if you feel very strongly that I should have set this to 1024, please comment below your reasoning. I fail to believe that a whole 512 block will be reserved for the new hash and and only use the additional 188 that I’ve set above. I like to believe the designers of NGINX wouldn’t allow such a block to be left… But I’m open to correction.

Once this has been changed, a server restart was required:

sudo service nginx restart

Love to hear if this worked for you!

 

Resources:

Leave a Reply

15 + seventeen =