nginx中的server_names_hash_max_size和server_names_hash_bucket_size

在一次给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.

也就是说:

为了快速处理数据的静态集合,像域名、映射指令的值、MIME类型、请求头的字符串名一类的数据,nginx使用了hash表。

在启动和重配置时,nginx会为hash表尽可能选用最小的值,

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

表的大小使用bucket表示,在hash表大小超过最大参数值之前,会持续调整。

大多数hash都有对应的指令,用来调整这些参数。例如:

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

设置hash桶大小的参数和处理器的缓存行大小的倍数有关,通过减少内存访问次数,可以提高现代处理器在hash中搜索密钥的速度。

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

 

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

导航