在一次給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 )