大网数据广告活动
服务器运维问答

How to specify several variables in the map directive of nginx

· 52CE 我爱测

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 direct…

问题原文

原帖链接 · zezollo · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · Alexey Ten · CC BY-SA 3.0

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";
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。