2

I'm using nginx version 1.4.6.

I'm having trouble getting map to work. I have something like this:

http {
    map $arg_f $forum {
        default 0;
        1 2;
    }

    # ...

    server {
        # ...

        location = /old.php {
            if ($forum) {
                return 301 /forums/$forum/;
            }
            return 494;
        }

        # ...
    }

    # ...
}

Here's what I get with nginx -t:

nginx: [emerg] unknown "forum" variable

The goal is to map old URIs to new URIs. The catch is that some of the numeric forum IDs have changed. I'd like to handle this in Nginx, so I'm willing to resort to lua if there's no other easy way. I'm open to alternative solutions as long as they're elegant, but I'd like to know why map isn't working here. Is it because the rewrite module doesn't query maps, at least in 1.4?

1

I just tested the following minimal configuration with nginx v1.4.7:

events {
    worker_connections  1024;
}


http {
    map $arg_f $forum {
        default 0;
        1 2;
    }

    server {
        location = /old.php {
            default_type  text/html;
            if ($forum) {
                return 200 "OK";
            }
            return 200 "KO";
        }
    }
}

It runs flawlessly, so the most probable cause of your trouble is that your configuration is not applied correctly. I would guess that changes you made on the side of adding the map directive broke it, making the forum variable inexistent.

Try to:

  1. Use nginx -t to validate your configuration
  2. Monitor your error log file while you issue service nginx reload to catch any error that might happen at runtime which are undetectable at configuration time.

As a side note, keep in mind that using up-to-date product is always a good idea.

  • At the very least, use nginx v1.4.7 Changelog
  • Even better, use at least the stable version (currently being v1.6.2) Changelog

http://nginx.org/en/download.html

  • I've tested the configuration pretty thoroughly. I always ensure that nginx -t passes. The map is in a separate configuration file that is definitely being included; there is nothing else in the configuration file. – Zenexer Nov 7 '14 at 4:29
  • What about errors popping up at reload time? Try to start from the configuration I provide to you, check it works as expected, then progressively modify it until you are back to your old config. Stop when configurations are the same or when an error occurs and use diff on files to try to narrow the problem down. Every difference (as 'simple' as include) is important – Bernard Rosset Nov 7 '14 at 18:04
0

Likely reason for that behaviour is that nginx -t won't load the variable before the restart, and won't be able to check if it's safe to use it resulting in unknown "x" variable error message.

Solution for that would be adding a map and restarting Nginx first, and then trying to use it and testing the changes with nginx -t.

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.