Nginx: could not build map_hash, you should increase map_hash_bucket_size: 64

19,019

Solution 1

map_hash_max_size and map_hash_bucket_size must be in http context, i.e. outside of map directive.

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

Solution 2

I put it on the top of the http {} and it worked. Don't put it outside http {}

Share:
19,019

Related videos on Youtube

Putnik
Author by

Putnik

AWS Certified Solutions Architect-Associate AWS Certified SysOps Administrator-Associate 10+ years with Linux etc :) Feel free to contact me via LinkedIn https://www.linkedin.com/in/alexander-lutchko-11b556b7/

Updated on September 18, 2022

Comments

  • Putnik
    Putnik almost 2 years

    I need to use redirect map with quite a lot rules (2k+ lines, file size is ~200K)

    I have the following settings in 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;
            }
    

    as described here and found here. The problem : configtest fails:

    nginx: [emerg] could not build map_hash, you should increase map_hash_bucket_size: 64
    

    I tried to increase map_hash_max_size and map_hash_bucket_size to quite crazy values:

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

    but still have the same error (ending exactly with '64'). I selected those values so they are bigger than the file size. I made sure I'm editing live config by adding "blabla" and see 'unknown directive'

    So, how those values should be set? There's not much details in the official doc, unfortunately.

  • Michael Hampton
    Michael Hampton over 5 years
    How does your answer differ from the existing answer?
  • Michael Hampton
    Michael Hampton over 5 years
    Well, having it twice in your config causes that problem.
  • tread
    tread over 5 years
    Yip. Another reason why it is better to put this setting in the main nginx config.
  • Kevin Nguyen
    Kevin Nguyen over 5 years
    I set up right above the map but still got the same error. It's really weird.
  • Alexey Ten
    Alexey Ten over 5 years
    @KevinNguyen what error?
  • Kevin Nguyen
    Kevin Nguyen over 5 years
    Nginx: could not build map_hash, you should increase map_hash_bucket_size: 64. I posted my answer btw.
  • Michael Hampton
    Michael Hampton about 5 years
    It is not obvious but it also needs to appear before the map directives.
  • black sensei
    black sensei almost 3 years
    @AlexeyTen this solution works well in with nginx 1.16 but not with nginx 1.18.