2019-05-17 nginx的map和split_clients模块、geo模块映射新的变量

map $http_host $name {
         hostnames;
         default 0;
         ~map.zhang\w+\.com 1;  #表示当请求的host匹配此规则时$name=1,默认$name=0
         *.aaa.com 2;
         map.aa.com 3;
         map.aaa.* 4;
}
map $http_user_agent $mobile {
         default 0;
         "~Opera Mini" 1;  #表示当请求的浏览器类型匹配此规则时 $mobile=1,默认$mobile=0
}
server {
      listen 8080;
      default_type text/plain;
      location / {
       return 200 '$name:$mobile\n';
      }

}
[root@hk conf.d]#curl -H 'host:map.aaa.cn' 127.0.0.1:8080
4:0
[root@hk conf.d]#curl -H 'host:*.aaa.com' 127.0.0.1:8080
2:0
[root@hk conf.d]#curl -H 'host:map.zhangdazhi.com' 127.0.0.1:8080
3:0

image.png
split_clients "${http_testcli}" $variant {  
       0.51% .one;  #表示请求的头部进行hash计算后/最大值的百分比落在此范围时 $variant=.one
       20.0% .two;
       50.5% .three;
       * "";
}
server {
      server_name split_clients.zhangdazhi.com;
        error_log /var/log/error.log debug;
        default_type text/plain;
        location / {
          return 200 'ABtestfile$variant\n';
         }
}
[root@hk conf.d]#nginx -s reload
[root@hk conf.d]#curl -H 'testcli:abcdefg' split_clients.zhangdazhi.com
ABtestfile
[root@hk conf.d]#curl -H 'testcli:abcdefghg' split_clients.zhangdazhi.com
ABtestfile.three

根据客户端地址创建新变量


image.png

image.png
geo $contury {
     default ZZ;
     #include conf/geo.conf;
    proxy 122.230.59.111;   #指定可信地址,此地址为要访问的客户端主机的真实ip,也就是X-Forwarded-For的第一个ip
    127.0.0.0/24 US; #当X-Forwarded-For的最后一个ip匹配此ip范围时,$contury=US
    127.0.0.1/32 RU;
    10.1.0.0/16 MU;
    192.168.1.0/24 UK;
 }

server {
    server_name geo.aaa.com;
    location / {
     return 200 '$contury\n';
 }
}
root@dell-PowerEdge-R730:/var/log/nginx# curl -H 'X-Forwarded-For:122.230.59.111,192.168.1.3' geo.aaa.com
UK
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容