Nginx – How to specify several variables in the map directive of nginx

configurationnginx

Well I have a slightly different question than this one: I don't want to map two variables at the same time, I want to map one variable using two others as parameters.

Indeed I have read in the documentation about the map directive that:

Before version 0.9.0 only a single variable could be specified in the
first parameter.

So, as I have nginx 1.8, I expect to be able to use more than a single variable in the first parameter.

This led me to think that I could write something like the two last map directives in this example:

map $http_user_agent $bot {
    default         "";
    "~*Googlebot"   "yes";
    "~*MJ12bot"     "yes";
    "~*bingbot"     "yes";
    etc.
}

map $request        $bot    $np {
    default         ""      "";
    default         "yes"   "";
    "~*newproject"  ""      "yes";
    "~*newproject"  "yes"   "";
}

map $bot    $np     $regular {
    ""      ""      "yes";
    ""      "yes"   "";
    "yes"   ""      "";
}

But it doesn't work (nginx -t returns a

invalid number of arguments in "map" directive

)

So, how is it possible to specify several variables in the first parameter? Or, what does actually mean the documentation, if I misunderstood it?

Best Answer

You have misunderstood nginx's docs. It means that before version 0.9 first argument of the map must be single variable and nothing else. Since version 0.9.0 first argument could be any string with any number of variables to interpolate.

Here how your first block could look like:

map "$bot:$request"      $np {
    default              "";
    "~^yes:"             "";
    "~*^:.*newproject"   "yes";
}