Detailed explanation of the configuration and use of Map module in Nginx server _nginx

Source: Internet
Author: User
Tags curl nginx server

The map instruction is provided using the Ngx_http_map_module module. By default, Nginx has to load this module, unless the artificial--without-http_map_module.
The Ngx_http_map_module module can create variables that are associated with values of other variables. Allowing classification or mapping of multiple values to multiple different values and storing them in a variable, the map instruction is used to create variables, but only when the variable is accepted the view mapping operation is performed, and the module does not have a performance flaw when handling requests that do not have reference variables.
A. Ngx_http_map_module Module instruction Description
Map
Syntax: Map $var 1 $var 2 {...}
Default value:-
Configuration segment: http
Map is the mapping table set for a variable. The mapping table consists of two columns, matching patterns and corresponding values.
The parameters in the map block specify the corresponding relationship between the source variable value and the result value.
A match pattern can be a simple string or regular expression, using a regular expression (' ~ ').
A regular expression that starts with "~" indicates that the regular expression is case sensitive. Begins with "~*", which indicates that the regular expression is insensitive to case-insensitive.

Map $http _user_agent $agent {
    default "";
    ~curl Curl;
    ~*apachebench "AB;
}

A regular expression can contain named captures and location captures, which can be used with the result variable by other directives.

Map $uri $value {
  /ttlsa_com          /index.php;
  ~^/ttlsa_com/(? <suffix>.*) $/boy/;
  ~/FZ (/.*)          /index.php?;              
}

[WARNING] cannot refer to a named capture or position capture variable inside a map block. such as ~^/ttlsa_com/(. *)/boy/$1; This will be an error nginx: [Emerg] Unknown variable. [/warning] If the value of the source variable contains a special word Furu ' ~ ', it is escaped with ' \ '.

Map $http _referer $value {
  Mozilla  ;
  \~mozilla 222;
}

The resulting variable can be either a string or another variable.

Map $num $limit {
     1 $binary _remote_addr;
     0 "";
}

The map directive has three parameters:
Default: Specifies the defaults that will be used if no results are matched. When default is not set, an empty string is used as the default result.
Hostnames: Allows a prefix or suffix mask to specify a domain name as the source variable value. This parameter must be written at the front of the list of value mappings.
Include: contains one or more files that contain mapped values.
If a match is made to a specific variable, such as a mask and a regular match, the selection is in the following order:
1. A string without a mask
2. The longest prefixed string, for example: "*.example.com"
3. The longest string with a suffix, for example: "Mail.*"
4. Regular expressions that first match in order (in the order in which they are represented in the configuration file)
5. Default value
Map_hash_bucket_size
Syntax: Map_hash_bucket_size size;
Default value: Map_hash_bucket_size 32|64|128;
Configuration segment: http
Specifies the maximum value of a variable in a mapping table in the hash table, depending on the processor's cache.
Map_hash_max_size
Syntax: Map_hash_max_size size;
Default value: Map_hash_max_size 2048;
Configuration segment: http
Sets the maximum value of the hash table corresponding to the map.
two. Examples

HTTP {
 Map $http _user_agent $agent {
 ~curl curl;
 ~*chrome Chrome;
 server {
    listen    8080;
    server_name test.ttlsa.com;
 
    Location/hello {
 default_type text/plain;
 echo http_user_agent: $http _user_agent;
 echo agent:agent: $agent;
 }}}

# Curl 127.0.0.1:8080/hello 
http_user_agent:curl/7.15.5 (X86_64-REDHAT-LINUX-GNU) libcurl/7.15.5 openssl/0.9.8b zlib/1.2.3 libidn/0.6.5
Agent:curl

HTTP {
 map $uri $match {
    ~^/hello/(. *) http://www.ttlsa.com/;
 }
 server {
    listen    8080;
    server_name test.ttlsa.com;
 
    Location/hello {
        default_type text/plain;
        echo URI: $uri;
        echo match: $match;
        echo Capture: $;
        Echo NEW: $match $}}}

PS: Speed limit Whitelist configuration based on map directives and GEO directives

HTTP {
 Geo $whiteiplist {
 default 1;
 127.0.0.1 0;
 10.0.0.0/8 0;
 121.207.242.0/24 0;
 }
 
 Map $whiteiplist $limit {
 1 $binary _remote_addr;
 0 "";
 }
 
 Limit_conn_zone $limit zone=limit:10m;
 
 server {
    listen    8080;
    server_name test.ttlsa.com;
 
    Location ^~/ttlsa.com/{
        limit_conn limit 4;
        Limit_rate 200k;
        alias/data/www.ttlsa.com/data/download/
    }}
 }

Technical points:
1. The GEO directive defines a whitelist $whiteiplist, with a default value of 1, all restricted. If the client IP matches the IP listed in the whitelist, the $whiteiplist value of 0 is unrestricted.
2. The map directive is a $whiteiplist value of 1, which is a restricted IP, mapped to client IP. The $whiteiplist value of 0, which is the white list IP, is mapped to an empty string.
3. The Limit_conn_zone and Limit_req_zone directives will be ignored for null values of the keys, thereby enabling the IP to be listed without restriction.
Test method:

# ab-c 100-n http://test.ttlsa.com:8080/ttlsa.com/docs/pdf/nginx_guide.pdf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.