Nginx ngx_http_map_module模块基本指令整理

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的ngx_http_map_module模块做简单介绍,本文具体包括如下指令:map,map_hash_bucket_size,map_hash_max_size

该ngx_http_map_module模块创建一个新变量,其值取决于其他变量的值。

配置示例如下:

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;

}

Nginx原文:

The ngx_http_map_module module creates variables whose values depend on values of other variables.

Example Configuration

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;

}

1. map

syntax:map           string$variable{ ... }
default:
context:http

创建一个新的变量,其值取决于一个或多个在第一个参数指定的源变量的值。

前版0.9.0只有一个变量可以在第一个参数指定。

Nginx原文:

依据声明中第一个变量的值根据整个语句的映射关系来创建另外一个变量(变量名是什么语句后面的名称,值前一变量值,根据映射关系进行映射的)。

在0.9.0版本前只有一个变量可以在第一个参数位置指定。

由于变量的实际计算仅仅是在需要时才计算, 因此声明再多的变量只要没有请求进来也不会造成额外的负担(从某种程度讲是有一定道理的, 但是在实际计算时,有过多的变量声明还是多需要些时间的,因此建议不需要的就不要写, 简化些更好!)

大括号中定义的参数被用来根据输入(源值)转换到输出值(被读取的值)

从0.9.6版本开始原值可以指定为值或者字符串的正则表达式。

对于大小写敏感的正则表达式应该以“~”符号开始,或者用“~ *”符号表示大小写不敏感的正则表达式。正则表达式可以包括命名的和位置的捕获(正则表示里面的术语,一般是用小括号括起来的部分),这些捕获可以在指令的后续部分使用。

如果源值匹配下面描述名字的一个特殊的参数,它应该在前面加上“\”符号。

所得到的值可以是字符串或另一个变量(0.9.0)。

指令也支持三个特殊参数:

默认值(default value)

如果原值变量没有相应的匹配, 则输出默认值。但没有指定默认值, 这输出空字符串

主机名(hostnames)

允许前缀或后缀掩码指定的主机名,作为源的值,例如,

*.example.com 1;

example.*     1;

下面俩个例子

example.com   1;

*.example.com 1;

can be combined:

.example.com  1;

这个参数应该在值列表中指定的。

包括文件

包括有价值的文件。可以有几种夹杂物。

如果源值匹配多个指定的变体,例如面具和正则表达式匹配,第一个匹配的变量将被选择,在下面的优先顺序:

这个参数应该在值列表中指定的。

包括文件(include file)

包含一个有值的文件, 可以有多个这样的文件

如果源值匹配多个指定的值,第一个匹配的变量将被选择,在下面的优先顺序:

1。没有掩码的字符串值

2。具有前缀掩模的最长字符串,例如“*.example.com”

3。具有后缀的最长字符串值,例如. “mail.*”4。最先匹配的正则表达式(在配置文件中出现的顺序)

5。默认值

Nginx原文:

Creates a new variable whose value depends on values of one or more of the source variables specified in the first parameter.

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

Since variables are evaluated only when used, the mere existence of even a large number of declared “map” variables does not incur any extra costs for request processing.

Parameters inside the map block specify a mapping between source and resulting values.

Source values are specified as strings or regular expressions (0.9.6).

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.

If a source value matches one of the names of special parameters described below, it should be prefixed with the “\” symbol.

The resulting value can be a string or another variable (0.9.0).

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

allows to specify hostnames with a prefix or suffix mask, as source values, for example,

*.example.com 1;

example.*     1;

The following two records

example.com   1;

*.example.com 1;

can be combined:

.example.com  1;

This parameter should be specified before the list of values.

include file

includes a file with values. There can be several inclusions.

If the source value matches more than one of the specified variants, e.g. both mask and regular expression match, the first matching variant will be chosen, in the following order of precedence:

1. string value without a mask

2. longest string value with a prefix mask, e.g. “*.example.com”

3. longest string value with a suffix mask, e.g. “mail.*”4. first matching regular expression (in order of appearance in a configuration file)

5. default value

 

2. map_hash_bucket_size

syntax:map_hash_bucket_size      size;
default:map_hash_bucket_size 32|64|128;
context:http

设置映射哈希表大小的桶。默认值取决于处理器的高速缓存行的大小。建立哈希表在一个单独的文件提供细节。

Nginx原文:

Sets the bucket size for the map variables hash tables. Default value depends on the size of the processor’s cache line. Details of setting up hash tables are provided in a separate document.

3. map_hash_max_size

syntax:map_hash_max_size     size;
default:map_hash_max_size 2048;
context:http

设置映射哈希表的最大大小。建立哈希表在一个单独的文件提供细节。

Nginx原文:

Sets the maximum size of the map variables hash tables. Details of setting up hash tables are provided in a separate document.

发表评论