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


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

附自己的一个实例:

 user              nginx;
worker_processes ; error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; events {
worker_connections ;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; # Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
# include /etc/nginx/conf.d/*.conf; proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
underscores_in_headers on;
# sendfile on;
tcp_nopush on; 61 map $http_iv$http_reserve $server {
62 default m6jifang;
63 b_103 gongyuan;
64 } upstream m6jifang {
server 10.1.1.23:8000 weight=1;
} upstream gongyuan {
server 59.108.107.96:14017 weight=1;
} server
{
listen 8080;
server_name ylyin.uuzz.com; location /yly-inceptor-web {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://$server;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Client $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} location /yly-channel-web {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://10.1.1.23:8000;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Client $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} 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' ; access_log /var/log/ylyin_acc.log ylyinlogs;
error_log /var/log/ylyin_error.log ; } }

map模块使用方法的更多相关文章

  1. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  2. thinkphp访问不存在的模块或者方法跳转到404页面

    使用的thinkphp 版本是3.2.0, 在config.php中配置 404地址,即可: 'TMPL_EXCEPTION_FILE' => './Application/Home/View/ ...

  3. ThinkPHP3.2判断手机端访问并设置默认访问模块的方法

    ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法 目录结构 公共模块Common,Home模块,Mobile模块 配置Application/Common/Conf/config.p ...

  4. apache2服务器mod_rewrite模块 开启方法[linux, ubuntu]

    在UBUNTU系统中要启用mod_rewrite的方法有两种: 第一种: 在终端中执行 sudo a2enmod rewrite 指 令后,即启用了 Mod_rewrite 模块, apache2服务 ...

  5. python在不同层级目录import模块的方法

    使用python进行程序编写时,经常会使用第三方模块包.这种包我们可以通过python setup install 进行安装后,通过import XXX或from XXX import yyy 进行导 ...

  6. 集合框架Map之entrySet方法的使用

    Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员 package cn ...

  7. 在JavaScript函数式编程里使用Map和Reduce方法

    所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...

  8. Map的遍历方法及String和其它类型的相互转化

    Map的遍历方法: package com.lky.test; import java.util.HashMap; import java.util.Iterator; import java.uti ...

  9. 使用Map/MapWhen扩展方法

    使用Map/MapWhen扩展方法 .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简 ...

随机推荐

  1. JUC集合之 JUC中的集合类

    Java集合包 在"Java 集合系列01之 总体框架"中,介绍java集合的架构.主体内容包括Collection集合和Map类:而Collection集合又可以划分为List( ...

  2. java 的关键字 native

    native native 关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中.Java语言本身不能对操作系统底层进行访问和操作,但是可 ...

  3. 添加BAUD_4800

    1.hal_uart.h 添加 #define HAL_UART_BR_4800 0x05 2.mt_uart.h 修改 #define MT_UART_DEFAULT_BAUDRATE HAL_UA ...

  4. 【python】正则表达式-group和group的区别

    __author__ = 'paul' import re a = "123abc456" print re.search("([0-9]*)([a-z]*)([0-9] ...

  5. PHP常用函数总结(一):

    <?php echo "<pre>"; //===============================时间日期======================== ...

  6. 关于 android 返回键 代码实现

    转自:http://www.dewen.io/q/11313/android+%E6%A8%A1%E6%8B%9F%E8%BF%94%E5%9B%9E%E9%94%AE%E5%8A%9F%E8%83% ...

  7. python使用分治法找序列最大值

    最近上算法导论课,说道分治法,回来想用python写写程序练练手,于是模仿一通写了如下的代码: __author__ = 'day' def ArrayMaxMin(Array): return ma ...

  8. Java-Runoob-高级教程-实例-数组:07. Java 实例 – 数组合并

    ylbtech-Java-Runoob-高级教程-实例-数组:07. Java 实例 – 数组合并 1.返回顶部 1. Java 实例 - 数组合并  Java 实例 以下实例演示了如何通过 List ...

  9. GIS(地理信息系统)

    ylbtech-杂项:GIS(地理信息系统) 地理信息系统(Geographic Information System或 Geo-Information system,GIS)有时又称为“地学信息系统 ...

  10. [UE4]通过代码改变材质

    OrangeMaterial = ConstructorStatics.OrangeMaterial.Get(); , OrangeMaterial); 使用到的结构体如下: struct FCons ...