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. 启用Win8/10(中文版/核心版/家庭版)中被阉割的远程桌面服务端

    Windows 8/8.1/10 标准版(中文版/核心版/家庭版)中取消了远程桌面服务端,想通过远程连接到自己的电脑就很麻烦了,第三方远程桌面速度又不理想(如TeamViewer).通过以下方法可让系 ...

  2. Linux Home目录硬盘空间缩减

    Linux Home目录硬盘空间缩减 操作   基于centos6.5 x86_64, runlevel 3,命令行模式,测试成功. 1.首先查看磁盘使用情况 [root@localhost ~]# ...

  3. opencv中的图像形态学——腐蚀膨胀

    腐蚀膨胀是图像形态学比较常见的处理,腐蚀一般可以用来消除噪点,分割出独立的图像元素等. 一般腐蚀操作对二值图进行处理,腐蚀操作如下图,中心位置的像素点是否与周围领域的像素点颜色一样(即是否是白色点,即 ...

  4. Centos 配置ifconfig命令

    刚装完CentOS 后ifconfig命令可能是不可用的,那么需要以下配置 ip addr vi /etc/sysconfig/network-scripts/ifcfg-xx (ONBOOT=yes ...

  5. css 填坑常用代码分享[居家实用型]

    原文地址 http://www.cnblogs.com/jikey/p/4233003.html 以下是常用的代码收集,没有任何技术含量,只是填坑的积累.转载请注明出处,谢谢. 一. css 2.x ...

  6. chrome和Firefox对p标签中单词换行的渲染(强制换行)

    谷歌和火狐对p标签单词的渲染: 今天在p标签展示url链接中,由于有几个下划线拼接的单词特别长, 所以总有那么几行老是超出p标签的范围,然后设置了强制 换行,才得以解决. word-wrap : br ...

  7. C++中,关于#include<***.h>和#include"***.h"的区别

    转载:天南韩立CSDN博客 #include<>直接从编译器自带的函数库中寻找文件 #include" "是先从自定义的文件中找 ,如果找不到在从函数库中寻找文件 采用 ...

  8. LINK : fatal error LNK1158: 无法运行“rc.exe”解决办法 and Visual Studio 2017 下载安装

    LINK : fatal error LNK1158: 无法运行“rc.exe” 首先下载软件包:https://pan.baidu.com/s/1L1N1sikXUaZZd-9nmZnwjA 第一个 ...

  9. ActiveMQ 高可用集群安装、配置(ZooKeeper + LevelDB)

    ActiveMQ 高可用集群安装.配置(ZooKeeper + LevelDB) 1.ActiveMQ 集群部署规划: 环境: JDK7 版本:ActiveMQ 5.11.1 ZooKeeper 集群 ...

  10. String intern()方法详解

    执行以下代码 String a1=new String("abc");       String a2=new String("abc");       Sys ...