> For the complete documentation index, see [llms.txt](https://docshome.gitbook.io/nginx-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docshome.gitbook.io/nginx-docs/readme/she-zhi-ha-xi.md).

# 设置哈希

为了快速处理静态数据集，如服务器名称、[map](http://nginx.org/en/docs/http/ngx_http_map_module.html#map) 指令值、MIME 类型、请求头字符串的名称，nginx 使用了哈希表。在开始和每次重新配置时，nginx 尽可能选择最小的哈希表，以便存储具有相同散列值的存储桶大小不会超过配置参数（哈希桶大小）。表的大小以桶为单位。调整是持续的，直到表的大小超过哈希的最大大小参数。大多数哈希具有修改这些参数对应的指令，例如，对于服务器名称哈希，它们是 [server\_names\_hash\_max](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_max_size) 和 [server\_names\_hash\_bucket\_size](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_bucket_size)。

哈希桶大小参数与处理器的高速缓存线大小的倍数对齐。可以通过减少内存访问的次数来加快现代处理器的哈希键搜索速度。如果哈希桶大小等于处理器的高速缓存线大小，则哈希键搜索期间内存访问次数最坏的情况下将有两次 —— 一是计算桶地址，二是在桶内搜索哈希键期间。因此，如果 nginx 发出消息请求增加到哈希的最大大小或者哈希桶的大小，那么首先应该增加第一个参数。

## 原文档

* <http://nginx.org/en/docs/hash.html>
