ngx_http_map_module用于創建一個變量,該變量的值依賴于其他變量的值。(The ngx_http_map_module module creates variables whose values depend on values of other variables.)
- 配置樣例
map $http_host $name {
hostnames;
default 0;
example.com 1;
*.example.com 1;
example.org 2;
*.example.org 2;
.example.net 3;
wap.* 4;
}
map $http_user_agent $mobile {
default 0;
"~Opera Mini" 1;
}
- 指令
Syntax: map string $variable { ... }
Default: -
Context: http
創建一新的變量。該變量的值,根據第一個參數中的一個或者多個來源變量的值來決定。(Creates a new variable whose value depends on values of one or more of the source variables specified in the first parameter.)
- 在0.9.0之前,第一個參數只能被指定為字符型變量。(Before version 0.9.0 only a single variable could be specified in the first parameter.)
- 因為變量是在被使用時才賦值,所以,僅僅聲明變量,不會增加額外的請求處理的成本,即使是大量的聲明。(Since variables are evaluated only when they are used, the mere declaration even of a large number of “map” variables does not add any extra costs to request processing.)
map block中的參數,指定了一種來源變量和目標變量之間的映射關系(Parameters inside the map block specify a mapping between source and resulting values.)
來源變量可以由字符串或者正則表達式(0.9.6)來表示(Source values are specified as strings or regular expressions (0.9.6).)
正則表達式可以由開始,表示對于區分大小寫,也可以由開始表示不區分大小寫(1.0.4)。正則表達式可以包括名字捕獲和位置捕獲,它們可以在隨后的關于目標變量的指令中用到。但是,不能在map的block中使用,否則會出現nginx: [emerg] unknown variable。(A regular expression should either start from the “~” symbol for a case-sensitive matching, or from the “~” symbols (1.0.4) for case-insensitive matching. A regular expression can contain named and positional captures that can later be used in other directives along with the resulting variable.)
如果一個來源變量名匹配XXX,應該加\前綴。(If a source value matches one of the names of special parameters described below, it should be prefixed with the “\” symbol.)
目標變量可以是字符串或者其他變量(0.9.0)。(The resulting value can be a string or another variable (0.9.0).)
指令還支持3中特殊參數:(The directive also supports three special parameters:)
默認(default value)
如果來源變量一個指令都沒有匹配到,則設置目標變量為默認值。如果沒有指定默認值,則目標變量會被賦值為一個空字符串。(sets the resulting value if the source value matches none of the specified variants. When default is not specified, the default resulting value will be an empty string.)
主機名(hostnames)
說明來源變量可以是帶前綴或者后綴的主機名。(indicates that source values can be hostnames with a prefix or suffix mask:)
*.example.com 1;
example.* 1;
以下的兩條記錄(The following two records)
example.com 1;
*.example.com 1;
可以合并成一條: (can be combined:)
.example.com 1;
包含(include file)
包括一個包含映射關系的文件。可以有多個inclusion。(includes a file with values. There can be several inclusions.)
如果匹配多個,比如同時匹配mask和正則表達式,那么會選擇mask作為匹配結果。(If the source value matches more than one of the specified variants, e.g. both a mask and a regular expression match, the first matching variant will be chosen, in the following order of priority:)
- 沒有掩碼的字符串變量(string value without a mask)
- 最長前綴匹配(longest string value with a prefix mask, e.g. “*.example.com”)
- 最長前綴匹配(longest string value with a suffix mask, e.g. “mail.*”)
- 第一個匹配的正則表達式(first matching regular expression (in order of appearance in a configuration file))
- 默認變量(default value)