ThisCalculator

Everyday

Fix Nginx Server Names Hash Bucket Size Error

Updated · Standard formula, shown below

Fix Nginx Server Names Hash Bucket Size Error

This guide explains how to resolve the common Nginx error could not build the server_names_hash, you should increase server_names_hash_bucket_size. You’ll learn what causes this error and how to fix it by adjusting a single directive in your Nginx configuration. No prior Nginx expertise is required, but you’ll need basic command-line access to your server.

What You’ll Need

  • Access to your server via SSH or a terminal.
  • Root or sudo privileges to edit Nginx configuration files.
  • Basic familiarity with the command line (e.g., using cd, nano, or vim).

Step-by-Step

  1. Identify the exact error message. Run nginx -t to test your configuration. You’ll see something like:
    nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64
    Note the number at the end (often 32, 64, or 128) – it indicates the current bucket size.
  2. Locate the main Nginx configuration file. Typically this is /etc/nginx/nginx.conf (Debian/Ubuntu) or /etc/nginx/conf/nginx.conf (RHEL/CentOS). You can also find it by running nginx -V and looking for the --conf-path argument.
  3. Open the configuration file with a text editor, e.g., sudo nano /etc/nginx/nginx.conf.
  4. Find the http block. It’s usually the top-level block that contains server and location directives. Look for a line like http {.
  5. Add or modify the server_names_hash_bucket_size directive. Inside the http block, add this line if it doesn’t exist, or change its value if it does:
    server_names_hash_bucket_size 64;
    If the error mentions a number higher than 64, use that number (e.g., 128). If the error doesn’t specify, start with 64.
  6. Save the file and test the configuration. Run nginx -t again. If the error persists, increase the value (e.g., to 128, then 256) and test again until the test passes.
  7. Reload Nginx to apply changes. Once nginx -t passes, run sudo systemctl reload nginx (or sudo service nginx reload on older systems). This applies the new setting without dropping connections.

Common Mistakes

  • Putting the directive in the wrong block. The directive must be inside http, not inside server or location blocks. If placed incorrectly, Nginx will ignore it or throw a different error.
  • Using a value that’s too small. If you set 32 when the error asks for 64, the problem won’t be fixed. Always start with the number from the error message or higher.
  • Forgetting to test after editing. Always run nginx -t before reloading to catch syntax errors that could take your server down.
  • Editing the wrong configuration file. Some setups include multiple config files in /etc/nginx/conf.d/ or /etc/nginx/sites-available/. The directive must be in the main nginx.conf under http, not in a site-specific file.

Tips

  • Understand what the directive does. Nginx uses a hash table to map server names (domain names) to server blocks. The bucket size is the memory allocation unit for this table. If your server names are long (e.g., very-long-subdomain.example.com), you may need a larger bucket size.
  • Use a power of two. The value should be a power of 2 (32, 64, 128, 256) for optimal performance, though Nginx accepts other values.
  • Consider also increasing server_names_hash_max_size. If you have many server names, you might also need to increase this (e.g., server_names_hash_max_size 512;). This is a separate directive but often mentioned alongside.
  • Keep server names short where possible. Reducing the length of domain names in your server_name directives can lower the required bucket size, but this is rarely practical.

Troubleshooting

If the error persists after increasing the value:

  • Check for other errors. Run nginx -t and look for any other messages. Sometimes the hash bucket error is a symptom of a syntax error elsewhere.
  • Verify the directive is in the correct file and block. Use grep -r "server_names_hash_bucket_size" /etc/nginx/ to see all occurrences. Ensure the one in nginx.conf is inside http { }.
  • Try a much larger value. If 64 or 128 doesn’t work, try 256 or 512. Some configurations with very long server names or many wildcard entries need a larger bucket.
  • Check for duplicate server_name entries. Duplicate server names can cause hash table issues. Use grep -r "server_name" /etc/nginx/ to spot duplicates.
  • If you’re using a control panel (cPanel, Plesk), the configuration might be generated automatically. You may need to add the directive to a custom include file or use the panel’s interface to edit Nginx settings.
  • If all else fails, restart Nginx instead of reloading. Run sudo systemctl restart nginx. This is more disruptive but can clear any cached state.

Frequently Asked Questions

What exactly does “server_names_hash_bucket_size” do?

It sets the memory allocation size for the hash table that stores server names (domain names) for virtual hosting. A larger bucket size allows Nginx to store longer server names without collisions, but uses slightly more memory.

Why does this error happen only when I add a new server block?

Adding a new server block with a longer or more complex server name can exceed the current hash table capacity. The default bucket size (often 32 or 64) may be too small for your specific set of domain names.

Is it safe to increase this value?

Yes, it’s safe. Increasing the bucket size only affects memory usage (by a few kilobytes at most) and has no negative impact on performance. It’s a standard fix recommended by Nginx documentation.

Can I set this directive in a server block instead of the http block?

No, the directive is only valid in the http context. Placing it elsewhere will cause a configuration error.

Do I need to restart Nginx or just reload?

Reload is sufficient because the configuration is re-read. However, if you’re also changing other core settings, a restart might be needed. Reload is less disruptive.

What if I don’t have access to the main nginx.conf file?

If you’re on a shared host, you may not be able to edit the main configuration. Contact your hosting provider and ask them to increase the server_names_hash_bucket_size. Some providers allow you to add custom Nginx directives via a control panel.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.