How to configure and use map module in Nginx server

10-19-2023

The map instruction is provided by the ngx_http_map_module module. By default, nginx loads this module unless it is artificial-without-http _ map _ module. The ngx_http_map_module module can create variables whose values are associated with other variable values. It is allowed to classify or map multiple values to multiple different values at the same time and store them in a variable. The map instruction is used to create variables, but the view mapping operation is only performed when the variables are accepted. There is no lack of performance in this module when handling requests without referencing variables. I. ngx_http_map_module module instruction Description Map syntax: map $var1 $var2 {...} Default value:-Configuration section: httpmap is a 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. The matching pattern can be a simple string or a regular expression. To use a regular expression, use ('~'). If a regular expression starts with ~, it means that the regular expression is case-sensitive. Starting with ~ * means that this regular expression is case-insensitive.

map $http_user_agent $agent { default "";  ~curl curl;  ~*apachebench" ab; }

Regular expressions can contain named capture and position capture, and these variables can be used by other instructions together with the result variables.

map $uri $value { /ttlsa_com  /index.php;  ~^/ttlsa_com/(? .*)$ /boy/;  ~/fz(/.*)  /index.php? ; }

Within a map block, named capture or location capture variables cannot be used. Such as ~/ttlsa _ com/(.*)/boy/$1; This will report the error nginx:


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: $1;  echo new: $match$1; } }}

2023052207540421436.jpg

Ps: White list configuration of speed limit based on map instruction and geo instruction

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. geo instruction defines a white list $whiteiplist, and the default value is 1, all of which are restricted. If the client ip matches the ip listed in the white list, the value of $whiteiplist is 0, which means there is no restriction. 2. The map instruction maps the $whiteiplist value of 1, that is, the restricted ip, to the client ip. Map the $whiteiplist with a value of 0, that is, the white list ip, to an empty string. 3. The limit _ conn _ zone and the limit_req_zone commands will be ignored if the key is null, so that the listed ip is not restricted. Test method:

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

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us