Nginx:无法构建 map_hash,你应该增加 map_hash_bucket_size: 64

Put*_*nik 8 configuration nginx hash sizing

我需要使用具有很多规则的重定向映射(2k+ 行,文件大小为 ~200K)

我在 nginx 中有以下设置:

map $uri $new_uri {
        include /etc/nginx/conf.d/redirects.map;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # redirects
        if ($new_uri) {
                rewrite ^ $new_uri permanent;
        }
Run Code Online (Sandbox Code Playgroud)

作为描述在这里,发现这里。问题:configtest 失败:

nginx: [emerg] could not build map_hash, you should increase map_hash_bucket_size: 64
Run Code Online (Sandbox Code Playgroud)

我试图将 map_hash_max_size 和 map_hash_bucket_size 增加到非常疯狂的值:

map $uri $new_uri {
        map_hash_max_size 262144;
        map_hash_bucket_size 262144;
        include /etc/nginx/conf.d/redirects.map;
}
Run Code Online (Sandbox Code Playgroud)

但仍然有相同的错误(完全以'64'结尾)。我选择了这些值,因此它们大于文件大小。我确保通过添加“blabla”并查看“未知指令”来编辑实时配置

那么,应该如何设置这些值呢?不幸的是,官方文档中没有太多细节。

Ale*_*Ten 15

map_hash_max_size并且map_hash_bucket_size必须在http上下文中,即在map指令之外。

map_hash_max_size 262144;
map_hash_bucket_size 262144;
map $uri $new_uri {
    include /etc/nginx/conf.d/redirects.map;
}
Run Code Online (Sandbox Code Playgroud)

  • @AlexeyTen 这个解决方案适用于 nginx 1.16,但不适用于 nginx 1.18。 (2认同)