Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure and use map Module in Nginx Server

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to configure and use the map module in the Nginx server". In the daily operation, I believe that many people have doubts about how to configure and use the map module in the Nginx server. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts about "how to configure and use the map module in the Nginx server". Next, please follow the editor to study!

The map instruction is provided by the ngx_http_map_module module. By default, nginx loads this module unless it is artificially-- without-http_map_module.

The ngx_http_map_module module can create variables whose values are associated with other variable values. Allows you to classify or simultaneously map multiple values to multiple different values and store them in a variable, the map instruction is used to create variables, but view mapping is performed only when the variables are accepted, and there is no performance deficiency in processing requests that do not reference variables.

one。 Ngx_http_map_module module instruction description

Map

Syntax: map $var1 $var2 {...}

Default value:-

Configuration segment: http

A mapping table set by map for a variable. The mapping table consists of two columns that match the pattern and the corresponding values.

The parameters in the map block specify the correspondence between the value of the source variable and the value of the result.

The matching pattern can be a simple string or regular expression, which is used ('~').

If a regular expression begins with "~", it indicates that the regular expression is case-sensitive. Starting with "~ *" indicates that the regular expression is case-insensitive.

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

Regular expressions can include named capture and location capture, and these variables can be used by other instructions along with the result variable.

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

[warning] you cannot reference named capture or location capture variables within a map block. For example, ~ ^ / ttlsa_com/ (. *) / boy/$1; will report an error nginx: [emerg] unknown variable. [/ warning] if the value of the source variable contains a special character such as'~', it is escaped with'\'.

Map $http_referer $value {mozilla 111;\ ~ mozilla 222;}

The result variable can be a string or another variable.

Map $num $limit {1 $binary_remote_addr; 0 ";}

The map directive has three parameters:

Default: specifies the default value that will be used if there is no match. When default is not set, an empty string will be used as the default result.

Hostnames: allows you to specify a domain name as the source variable value with a prefix or suffix mask. This parameter must be written at the top of the value mapping list.

Include: contains one or more files containing mapped values.

If multiple specific variables are matched, such as mask and regular matching at the same time, the selection is made in the following order:

1. A string without a mask

two。 The longest prefixed string, for example: "* .example.com"

3. The longest suffixed string, for example: "mail.*"

4. The first regular expression to match first in order (the order reflected 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 mapping table.

two。 Example

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/hellohttp_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.5agent: 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: $1; echo new: $match$1;}

Ps: speed limit whitelist configuration based on map instruction and geo instruction

Http {geo $whiteiplist {default 1; 127.0.0.1 0; 10.0.0.0 ttlsa.com/ 8 0; 121.207.242.0 binary_remote_addr; 240;} 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 of $whiteiplist, with a default value of 1, all of which are restricted. If the client ip matches the ip listed in the whitelist, the $whiteiplist value is 0, which means there is no limit.

2. The map directive maps the $whiteiplist value to 1, that is, the restricted ip, to the client ip. The whitelist ip with a value of 0, whitelist, is mapped to an empty string.

3. Limit_conn_zone and limit_req_zone instructions will be ignored if the key is null, so that there is no limit to the listed ip.

Test method:

# ab-c 100-n 300 http://test.ttlsa.com:8080/ttlsa.com/docs/pdf/nginx_guide.pdf at this point, the study on "how to configure and use map modules in the Nginx server" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report