How To Fix "[emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size" Nginx Error?

Published July 21, 2024

Problem: Nginx Server Names Hash Error

The "[emerg] could not build the server_names_hash" error in Nginx happens when the server can't allocate enough memory for the hash table that stores server names. This problem usually occurs when there are too many server names or very long server names in the Nginx setup.

Solving the Nginx Server Names Hash Issue

Increasing the server_names_hash_bucket_size

To fix the server names hash error in Nginx, you need to increase the server_names_hash_bucket_size value. Here's how to do it:

  1. Open your Nginx configuration file:

    sudo nano /etc/nginx/nginx.conf
  2. Find the http block and add or modify this line:

    server_names_hash_bucket_size 64;
  3. If the error continues, try increasing the value to 128 or the next power of two.

  4. Save the file and exit the text editor.

  5. Test the new configuration:

    sudo nginx -t
  6. If the test passes, restart Nginx:

    sudo systemctl restart nginx

The suggested starting value for server_names_hash_bucket_size is 64. You may need to adjust this based on your setup. If you have many server names or long domain names, you might need to increase it more.

After making changes, always test your configuration before restarting Nginx. This helps prevent downtime caused by configuration errors.

Tip: Optimize server_names_hash_max_size

If increasing server_names_hash_bucket_size doesn't solve the issue, you might also need to adjust the server_names_hash_max_size. Add this line to your http block:

server_names_hash_max_size 512;

This value should be increased if you have a large number of server names. Start with 512 and increase as needed.

Alternative Solutions for Nginx Server Name Issues

Optimizing server block configurations

To make your Nginx server block configurations better, try these tips:

  1. Group similar server names: Combine server blocks with similar configurations to reduce the number of separate blocks.

  2. Use location blocks: Use location blocks to handle specific URL patterns, reducing the need for multiple server blocks.

  3. Remove unused server names: Delete any server names you no longer use to minimize the hash table size.

  4. Use shorter server names: When possible, use shorter domain names to reduce the overall length of server names.

  5. Implement server name aliases: Use server_name directives with multiple domain names to handle variations of the same site.

Tip: Use Regular Expressions for Server Names

Utilize regular expressions in your server_name directive to match multiple domains with a single rule. For example:

server_name ~^(www\.)?example\.(com|org|net)$;

This matches example.com, www.example.com, example.org, www.example.org, example.net, and www.example.net.

Using wildcard server names

Wildcard server names in Nginx let you match multiple subdomains with a single server block. This can help reduce the number of server blocks and simplify your configuration.

To implement wildcard server names in Nginx:

  1. Open your Nginx configuration file:

    sudo nano /etc/nginx/nginx.conf
  2. Add a server block with a wildcard server name:

    server {
       listen 80;
       server_name *.example.com;
       # Other configuration directives
    }
  3. You can also use more specific wildcard patterns:

    server_name www.*.example.com;
    server_name *.example.*;
  4. Save the file and exit the text editor.

  5. Test the new configuration:

    sudo nginx -t
  6. If the test passes, restart Nginx:

    sudo systemctl restart nginx