map模块使用方法

map指令使用ngx_http_map_module模块提供的。默认情况下,nginx有加载这个模块,除非人为的 --without-http_map_module。
ngx_http_map_module模块可以创建变量,这些变量的值与另外的变量值相关联。允许分类或者同时映射多个值到多个不同值并储存到一个变量中,map指令用来创建变量,但是仅在变量被接受的时候执行视图映射操作,对于处理没有引用变量的请求时,这个模块并没有性能上的缺失。

一. ngx_http_map_module模块指令说明

map
语法: map $var1 $var2 { ... }
默认值: —
配置段: http
map为一个变量设置的映射表。映射表由两列组成,匹配模式和对应的值。
在 map 块里的参数指定了源变量值和结果值的对应关系。
匹配模式可以是一个简单的字符串或者正则表达式,使用正则表达式要用('~')。
一个正则表达式如果以 “~” 开头,表示这个正则表达式对大小写敏感。以 “~*”开头,表示这个正则表达式对大小写不敏感。

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

正则表达式里可以包含命名捕获和位置捕获,这些变量可以跟结果变量一起被其它指令使用。

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

[warning]不能在map块里面引用命名捕获或位置捕获变量。如~^/ttlsa_com/(.*)  /boy/$1; 这样会报错nginx: [emerg] unknown  variable。[/warning]如果源变量值包含特殊字符如‘~’,则要以‘’来转义。

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

结果变量可以是一个字符串也可以是另外一个变量。

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

map指令有三个参数:
default : 指定如果没有匹配结果将使用的默认值。当没有设置 default,将会用一个空的字符串作为默认的结果。
hostnames : 允许用前缀或者后缀掩码指定域名作为源变量值。这个参数必须写在值映射列表的最前面。
include : 包含一个或多个含有映射值的文件。

如果匹配到多个特定的变量,如掩码和正则同时匹配,那么会按照下面的顺序进行选择:
1. 没有掩码的字符串
2. 最长的带前缀的字符串,例如: “*.example.com”
3. 最长的带后缀的字符串,例如:“mail.*”
4. 按顺序第一个先匹配的正则表达式 (在配置文件中体现的顺序)
5. 默认值

map_hash_bucket_size
语法: map_hash_bucket_size size;
默认值: map_hash_bucket_size 32|64|128;
配置段: http
指定一个映射表中的变量在哈希表中的最大值,这个值取决于处理器的缓存。

map_hash_max_size
语法: map_hash_max_size size;
默认值: map_hash_max_size 2048;
配置段: http
设置映射表对应的哈希表的最大值。

二. 实例

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

map
map

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;
        }
    }
}

map

 

 

附自己的一个实例: 

  1 user              nginx;
  2 worker_processes  8;
  3 
  4 error_log  /var/log/nginx/error.log;
  5 #error_log  /var/log/nginx/error.log  notice;
  6 #error_log  /var/log/nginx/error.log  info;
  7 
  8 pid        /var/run/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       /etc/nginx/mime.types;
 18     default_type  application/octet-stream;
 19 
 20     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21                       '$status $body_bytes_sent "$http_referer" '
 22                       '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     access_log  /var/log/nginx/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33     
 34     # Load config files from the /etc/nginx/conf.d directory
 35     # The default server is in conf.d/default.conf
 36     # include /etc/nginx/conf.d/*.conf;
 37 
 38 
 39 
 40     proxy_redirect          off; 
 41     proxy_set_header        Host $host; 
 42     proxy_set_header        X-Real-IP $remote_addr;
 43     proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for;
 44     client_max_body_size    10m; 
 45     client_body_buffer_size 128k; 
 46     proxy_connect_timeout   90; 
 47     proxy_send_timeout      90; 
 48     proxy_read_timeout      90; 
 49     proxy_buffer_size       4k; 
 50     proxy_buffers           4 32k; 
 51     proxy_busy_buffers_size 64k; 
 52     proxy_temp_file_write_size 64k;
 53     server_names_hash_bucket_size 128; 
 54     client_header_buffer_size 32k; 
 55     large_client_header_buffers 4 128k;    
 56     underscores_in_headers on;
 57 #   sendfile        on;
 58     tcp_nopush     on;
 59     
 60 
 61     map $http_iv$http_reserve  $server {
 62         default m6jifang;
 63         b_103  gongyuan;
 64     }
 65 
 66 
 67     upstream m6jifang {
 68         server 10.1.1.23:8000 weight=1;
 69 }
 70    
 71 
 72     upstream gongyuan {
 73         server 59.108.107.96:14017 weight=1;
 74 }
 78 
 79 server
 80     {
 81     listen  8080;
 82     server_name  ylyin.uuzz.com;
 83        
 84     location /yly-inceptor-web   {        
 85             proxy_next_upstream http_502 http_504  error timeout invalid_header;
 86             proxy_pass         http://$server;
 87             proxy_set_header   Host             $host;
 88             proxy_set_header   X-Real_IP        $remote_addr;
 89             proxy_set_header   X-Client         $remote_addr;
 90             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 91      }
 92 
 93     location /yly-channel-web   {        
 94             proxy_next_upstream http_502 http_504  error timeout invalid_header;
 95             proxy_pass         http://10.1.1.23:8000;
 96             proxy_set_header   Host             $host;
 97             proxy_set_header   X-Real_IP        $remote_addr;
 98             proxy_set_header   X-Client         $remote_addr;
 99             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
100     } 
101 
102     log_format  ylyinlogs  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' "$http_user_agent" '$http_x_forwarded_for' '$http_iv $http_reserve' ;
103 
104 
105         access_log  /var/log/ylyin_acc.log  ylyinlogs;
106         error_log   /var/log/ylyin_error.log  ;
107 
108 }
109 
110 }

 

 

原文地址:https://www.cnblogs.com/wjoyxt/p/4606736.html