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.


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;
  • I set up right above the map but still got the same error. It's really weird. – Kevin Nguyen Nov 19 '18 at 12:47
  • @KevinNguyen what error? – Alexey Ten Nov 19 '18 at 13:52
  • Nginx: could not build map_hash, you should increase map_hash_bucket_size: 64. I posted my answer btw. – Kevin Nguyen Nov 19 '18 at 17:27
  • It is not obvious but it also needs to appear before the map directives. – Michael Hampton Apr 23 '19 at 18:05
  • @AlexeyTen this solution works well in with nginx 1.16 but not with nginx 1.18. – black sensei Jul 1 at 15:46

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


Add to the top of the http block in the main configuration file (/etc/nginx/nginx.conf):

map_hash_bucket_size 128;
  • How does your answer differ from the existing answer? – Michael Hampton Nov 18 '18 at 21:10
  • I got the following error: nginx: "map_hash_bucket_size" directive is duplicate in /etc/nginx/conf.d/my-redirect-map.conf:2 using the above answer. This answer indicates that map_hash_max_size is not required and this global setting is better suited to be placed in the main configuration file. – stephen Nov 18 '18 at 22:10
  • Well, having it twice in your config causes that problem. – Michael Hampton Nov 18 '18 at 22:25
  • Yip. Another reason why it is better to put this setting in the main nginx config. – stephen Nov 18 '18 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.