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. 标 题: 有什么办法快速把pc上的网址发送到手机上

    标  题: 有什么办法快速把pc上的网址发送到手机上 transfer2u, pushbullet都可以实现你说的功能,还可以把图片或者选中内容/剪贴板内容发送到手机.后者功能更强,还支持在电脑之间发 ...

  2. netty 知识点

    Netty:异步.事件驱动的NIO(非阻塞Non-blocking IO)框架 netty 教程:http://wiki.jikexueyuan.com/project/netty-4-user-gu ...

  3. jvm 知识点

    双亲委派模型的工作流程是: 如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把请求委托给父加载器去完成,依次向上,因此,所有的类加载请求最终都应该被传递到顶层的启动类加载器中, ...

  4. C#继承基本控件实现自定义控件 (转帖)

    自定义控件分三类: 1.复合控件:基本控件组合而成.继承自UserControl 2.扩展控件:继承基本控件,扩展一些属性与事件.比如继承Button 3.自定义控件:直接继承自Control 第一种 ...

  5. 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示

    一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...

  6. 最新hadoop虚拟机安装教程(附带图文)

    前两天看到有人留言问在什么情况下需要部署hadoop,我给的回答也很简单,就是在需要处理海量数据的时候才需要考虑部署hadoop.关于这个问题在很早之前的一篇分享文档也有说到这个问题,数据量少的完全发 ...

  7. JSSDK微信自定义分享朋友圈

    服务项目 新手技术咨询 企业技术咨询 定制开发 服务说明 QQ有问必答 QQ.微信.电话 微信开发.php开发,网站开发,系统定制,小程序开发 价格说明 200元/月 1000/月 商议       ...

  8. 【Active入门】ActiveMQ学习-1

    基本概念: 接收者: package com.ll.activemq; import java.util.Collection; import java.util.Iterator; import j ...

  9. python2.7中出现TypeError: must be type, not classobj

    class Person: def __init__(self,name,age): self._name = name self._age = age class Student(Person): ...

  10. Makefile | Linux嵌入式编程 使用详细图解

    针对的是对Makefile一点都不会的小白哦! 练习之前我们要做好准备: (1):第一步创建一个目录,因为实验过程中生成的文件会很多,不要把你系统里的文件搞得乱七八糟. [cjj@bogon ~]$ ...