ngx_http_map_module用于創(chuàng)建一個(gè)變量瓦堵,該變量的值依賴于其他變量的值借帘。(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
創(chuàng)建一新的變量。該變量的值济蝉,根據(jù)第一個(gè)參數(shù)中的一個(gè)或者多個(gè)來源變量的值來決定杰刽。(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之前,第一個(gè)參數(shù)只能被指定為字符型變量王滤。(Before version 0.9.0 only a single variable could be specified in the first parameter.)
- 因?yàn)樽兞渴窃诒皇褂脮r(shí)才賦值贺嫂,所以,僅僅聲明變量雁乡,不會(huì)增加額外的請(qǐng)求處理的成本第喳,即使是大量的聲明。(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中的參數(shù)踱稍,指定了一種來源變量和目標(biāo)變量之間的映射關(guān)系(Parameters inside the map block specify a mapping between source and resulting values.)
來源變量可以由字符串或者正則表達(dá)式(0.9.6)來表示(Source values are specified as strings or regular expressions (0.9.6).)
正則表達(dá)式可以由開始曲饱,表示對(duì)于區(qū)分大小寫悠抹,也可以由開始表示不區(qū)分大小寫(1.0.4)。正則表達(dá)式可以包括名字捕獲和位置捕獲渔工,它們可以在隨后的關(guān)于目標(biāo)變量的指令中用到锌钮。但是,不能在map的block中使用引矩,否則會(huì)出現(xiàn)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.)
如果一個(gè)來源變量名匹配XXX贝淤,應(yīng)該加\前綴范咨。(If a source value matches one of the names of special parameters described below, it should be prefixed with the “\” symbol.)
目標(biāo)變量可以是字符串或者其他變量(0.9.0)。(The resulting value can be a string or another variable (0.9.0).)
指令還支持3中特殊參數(shù):(The directive also supports three special parameters:)
默認(rèn)(default value)
如果來源變量一個(gè)指令都沒有匹配到酝惧,則設(shè)置目標(biāo)變量為默認(rèn)值区端。如果沒有指定默認(rèn)值值漫,則目標(biāo)變量會(huì)被賦值為一個(gè)空字符串。(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.)
主機(jī)名(hostnames)
說明來源變量可以是帶前綴或者后綴的主機(jī)名织盼。(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)
包括一個(gè)包含映射關(guān)系的文件杨何。可以有多個(gè)inclusion沥邻。(includes a file with values. There can be several inclusions.)
如果匹配多個(gè)危虱,比如同時(shí)匹配mask和正則表達(dá)式,那么會(huì)選擇mask作為匹配結(jié)果唐全。(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)
- 最長(zhǎng)前綴匹配(longest string value with a prefix mask, e.g. “*.example.com”)
- 最長(zhǎng)前綴匹配(longest string value with a suffix mask, e.g. “mail.*”)
- 第一個(gè)匹配的正則表達(dá)式(first matching regular expression (in order of appearance in a configuration file))
- 默認(rèn)變量(default value)