In use map nginx

Transfer from https://blog.51cto.com/tchuairen/2175525?source=dra

map command description:

map ngx_http_map_module instructions are provided by the module, the module will be installed nginx installation default.

The main role is to create a custom map variables by using nginx's built-in variables, to match the specific rule, if the match is successful then set a value for the custom variable. And this custom variable and can make his use.

Direct look at an example to understand more clearly:

  • Scene: url parameters matching the request, if the debug parameter is set $ foo = 1, the default setting $ foo = 0
map $args $foo {
    default 0;
    debug   1;
}

 

Explanation:

$ Args is nginx built-in variables, parameters of the request url is obtained. If $ args match to debug then the value of $ foo will be set to 1, if a match less than $ args $ foo is the default value defined here is 0

map syntax

map $var1 $var2 {...}

 

Three parameters map instructions:

1, default: specify the source variables not match the default values ​​will be used when any expression. When not set default, it will use an empty string as a result of the default.

2, hostnames: allows you to specify suffix or prefix mask of variable values ​​as the source domain. This parameter value must be written at the top of the list of maps.

3, include: file containing one or more maps containing values.

  • Role section in the Nginx configuration file: http {}, attention map can not be written in the server {} otherwise it will error

map as the source of $ var1 variable, usually can be nginx built-in variable, $ var2 custom variables. Value depends on the matching of $ $ var2 var1 corresponding expression. If a match is less than var2 corresponding default value is $.

  • If a regular expression starts with "~", indicating that the regular expression is case sensitive. Beginning with "~ *" indicates that the regular expression is not case sensitive.
map $http_user_agent $agent {
    default "";
    ~curl curl;
    ~*apachebench" ab;
}    

 

  • Regular expressions can contain name and location of capture capture, these variables can be used together with the results of other variables instruction.
map $uri $value {
    /abc                       /index.php;
    ~^/teacher/(?<suffix>.*)$  /boy/;
    ~/fz(/.*)                  /index.php?fz=1;                           
}

 

== Note: The name can not reference variables capture or capture position inside the block map. The ~ ^ / qupeicom /(.*) / peiyin / $ 1; this will be given nginx: [emerg] unknown variable ==

== Note 2: If the source variable value contains special characters such as '-', will have the '\' to escape. ==

map $http_referer $value {
    Mozilla    111;
    \~Mozilla  222;
}

 

  • Source matching expression variable corresponding to the result value can be a string that can be another variable.
map $http_referer $value {
    Mozilla    'chrom';
    \~safity    $http_user_agent;

 

Example (a)

  • Use the map to implementation issues allows multiple domain names cross-domain access
If it is allowed to access cross-domain single-domain configuration directly on the line, as follows:
# These configurations can write http {} or server {} are supported. 
Access the add_header -Control-the Allow-Origin " http://www.tutu.com " ; 
the add_header Access -Control-the Allow-Methods " the POST, the GET, the PUT, the OPTIONS, the DELETE " ; 
the add_header Access -Control-Max-Age " 3600 " ; 
add_header access -Control-the allow-Headers " Origin, the X-Requested-With-, Content-Type, the Accept;
 
# / bin /! bash
 # above configuration allows only HTTP: // www.tutu.com cross-domain access, If you want to support all domain names can be cross-domain call the station. the top row into this, but this is not recommended, because of insecurity 
add_header Access -Control-the Allow-Origin " *";

 

If you do not want to allow all, but they need to allow multiple domain names, then you need to use the map
map $http_origin $corsHost {
    default 0;
    "~http://www.haibakeji.com" http://www.haibakeji.com;
    "~http://m.haibakeji.com" http://m.haibakeji.com;
    "~http://wap.haibakeji.com" http://wap.haibakeji.com;
}
server
{
    listen 80;
    server_name www.haibakeji.com;
    root /nginx;
    location /
    {
        add_header Access-Control-Allow-Origin $corsHost;
    }
}

 

Examples of (b)

  • Use the source variable (usually nginx built-in variable) to match some of the rules, create custom variables, then the page output. This is often useful when debugging

    http {
    map $uri $match {
        ~^/hello/(.*) http://www.hello.com/;
    }
    server {
        listen       8080;
        server_name  test.hello.com;
    
        location /hello {
                default_type text/plain;
                echo uri: $uri;
                echo match: $match;
                echo capture: $1;
                echo new: $match$1;
        }

     

Performance issues related to map

We may have a problem, map since it can only be used at http segment, which is global ah. This setting will have access to all virtual host requests must match again and set the variable's value, however, is not the case, the request is not used for related variables, it does not perform map operations. There is no loss of performance.

Match Priority issues

If a match to a more specific variables, such as masks and also matches a regular, it will be selected in the following order:

  1. No mask string
  2. The longest prefix string with, for example: "* .example.com"
  3. The longest string of a suffix, for example: "mail *."
  4. In order to match the first regular expression (sequence embodied in a configuration file)
  5. Defaults

map_hash_bucket_size

  • 语法: map_hash_bucket_size size;
  • Default: map_hash_bucket_size 32 | 64 | 128;
  • Configuration section: http
  • Specify a maximum value in the mapping table variable in the hash table, the value depending on the processor's cache.

  • map_hash_max_size
  • 语法: map_hash_max_size size;
  • Default value: map_hash_max_size 2048;
  • Configuration section: http
  • The mapping table corresponding to the maximum hash table.

Guess you like

Origin www.cnblogs.com/cangqinglang/p/12174407.html