在一次给nginx添加完配置文件后,使用 nginx -t 检查配置文件语法时,nginx报告如下内容: 

1 nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 256; ignoring server_names_hash_bucket_size


在nginx.con中 调大 server_names_hash_max_size 和 server_names_hash_bucket_size 的值的大小:例如

    server_names_hash_max_size      1024;  
    server_names_hash_bucket_size   512;


遇到问题不要慌,先去查一下nginx的官方文档看看 server_names_hash_max_size  和 server_names_hash_bucket_size  是做什么的:

ConfigMap Key Description Default
server-names-hash-bucket-size Sets the value of the server_names_hash_bucket_size directive. 256
server-names-hash-max-size Sets the value of the server_names_hash_max_size directive. 1024


Syntax: server_names_hash_bucket_size size;
Default:    server_names_hash_bucket_size 32|64|128;
Context:    http
Sets the bucket size for the server names hash tables. 
The default value depends on the size of the processor’s cache line.
The details of setting up hash tables are provided in a separate document. 意思就是说: 用于设置域名哈希表的桶(bucket)的大小。默认值由处理器的缓存行来决定,详细信息可以在单独的文档中找到。

Syntax: server_names_hash_max_size size; Default: server_names_hash_max_size 512; Context: http Sets the maximum size of the server names hash tables. The details of setting up hash tables are provided in a separate document.


Setting up hashes

To quickly process static sets of data such as server names, map directive’s values, MIME types, names of request header strings, nginx uses hash tables. During the start and each re-configuration nginx selects the minimum possible sizes of hash tables such that the bucket size that stores keys with identical hash values does not exceed the configured parameter (hash bucket size). The size of a table is expressed in buckets. The adjustment is continued until the table size exceeds the hash max size parameter. Most hashes have the corresponding directives that allow changing these parameters, for example, for the server names hash they are server_names_hash_max_size and 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. This speeds up key search in a hash on modern processors by reducing the number of memory accesses. If hash bucket size is equal to one processor’s cache line size then the number of memory accesses during the key search will be two in the worst case — first to compute the bucket address, and second during the key search inside the bucket. Therefore, if nginx emits the message requesting to increase either hash max size or hash bucket size then the first parameter should first be increased.




这样存储具有同类哈希值的键的桶的大小不会超过设置的参数值(hash bucket size 哈希桶大小)。



域名hash 对应的指令是  server_names_hash_max_size 和 server_names_hash_bucket_size


如果一个哈希桶的大小等于一个处理器的缓存行的大小,那么在最坏的情况下,搜索key时会访问两次内存: 第一次是计算哈希桶的位置,其次是在哈希桶中搜索key时。如果nginx发出请求增加 最大哈希值( hash max size )哈希桶的大(hash bucket size) 的消息,那么首选应该增加 最大哈希值( hash max size )


posted on 2020-07-22 16:52  R_e  阅读(3033)  评论(0编辑  收藏  举报