Skip to content

Instantly share code, notes, and snippets.

@muhammadghazali
Last active February 9, 2024 14:13
Show Gist options
  • Save muhammadghazali/6c2b8c80d5528e3118613746e0041263 to your computer and use it in GitHub Desktop.
Save muhammadghazali/6c2b8c80d5528e3118613746e0041263 to your computer and use it in GitHub Desktop.
Nginx: What is server_names_hash_bucket_size?

Background

While I'm learning how to use Nginx, I was instructed to update the server_names_hash_bucket_size (/etc/nginx/nginx.conf) value from 32 to 64, but I don't understand why should I increase the value to 64.

References

References that have been read so far:

Key takeaways

  • The default value of server_names_hash_bucket_size depends on the size of the processor’s cache line
  • If a large number of server names are defined, or unusually long server names are defined, tuning the server_names_hash_max_size and server_names_hash_bucket_size directives at the http level may become necessary.
  • If the default value of server_names_hash_bucket_size used at the server is not enough, means nginx complained with could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32, the directive value should be increased to the next power of two (e.g. in this case to 64).
  • If a large number of server names are defined, and nginx complained with the following error could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 32. try to set server_names_hash_max_size to a number close to the number of server names. Only if this does not help, or if nginx’s start time is unacceptably long, try to increase server_names_hash_bucket_size.
  • The hash bucket size parameter is aligned to the size that is a multiple of the processor’s cache line size
@DougBeney
Copy link

Thanks for creating this!

@mattbontrager
Copy link

Hear Hear!

@hemantgoyal
Copy link

Thanks for this. Can you also please the difference between the max and the bucket size ? It still confuses me.

@Raymond26
Copy link

Thanks for this!

@kowalski-relent
Copy link

Thanks for this!

@sto1t
Copy link

sto1t commented Mar 6, 2021

√ Still useful. Thanks!

@inoas
Copy link

inoas commented May 6, 2021

Well I raised both values to powers of 2 in steps till 512 and it did nothing.
Shortening very long local development domains worked.

ROLE-dashboard-COMPANY-TLD-docker-FRAMEWORK.local + 4 letters port => to something shorter fixed it for me.

@Meekohi
Copy link

Meekohi commented May 7, 2021

Kind of odd nginx can't just resize this on the fly. Updating from 32 to 256 worked for me.

@CiderAndWhisky
Copy link

Thank you!

@SDS712
Copy link

SDS712 commented Apr 13, 2022

Thanks

@dermanov-ru
Copy link

Thank you!

@RonanKER
Copy link

RonanKER commented Aug 2, 2023

Well I raised both values to powers of 2 in steps till 512 and it did nothing. Shortening very long local development domains worked.

ROLE-dashboard-COMPANY-TLD-docker-FRAMEWORK.local + 4 letters port => to something shorter fixed it for me.

With only 50 subdomains I had an NGinx crash with this error "could not build server_names_hash, you should increase server_names_hash_bucket_size: 64" just after adding a new one.

Investigating the issue : the last sub-domain added was 32 carracters long (total server_name with domain&tld = 48)

  • if I shorten it to 30 (total 46) : it works.
  • if I change server_names_hash_bucket_size from 64 to 128 : it works.

I don't understand why the length of server names is limited by this parameter, i'm still far from the official (RFC) limits for URLs. (and very very far from hash collisions)

I need to have a close look on this topic because in my case several subdomains are created arround a client choice for the "base name", so I need to know wich limit should I impose for this "base name".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment