Nginx基于客户端请求头的访问分类

 

 

   Nginx0.8.37版本开始提供一个叫做ngx_http_split_clients_module模块,由该模块的功能从它名字就很容易看出,因此,它的功能就是用基于某些条件(例如,IP地址、头、cookie,等等)将客户端访问的资源分开。

   

配置示例

 

http {

    split_clients "${remote_addr}AAA" $variant {

        0.5% .one;

        2.0% .two;

        * "";

    }

 

    server {

        location / {

             index index${variant}.html;

 

   

   

 

    该模块只提供了1条命令。

 

指令名称:split_clients

    能:该指令用于对哈希源数据计算,从1.0.1版本之后的Nginx,该指令对源字符串进行计算时使用的不再是CRC32,而使用改为使用MurmurHash2算法,然后会根据哈希的百分比作为员的值。

    : split_clients  source_hash  $variable { ... }

默 认 值: none

使用环境: http

 

   

 

    该模块提供了一个变量,那就是$variant

 

变量名称:$variant

    能:通过它的值就会代表着访问者最终访问的去处。

 

使用实例

 

    Nginx服务器的配置文件中添加以下配置内容:

 

http {

  include       mime.types;

  default_type  application/octet-stream;

 

  sendfile           on;

 

  keepalive_timeout  65;

 

  log_format custom $time_local | $server_name | $request_length | $bytes_sent | $remote_addr | $variant;

 

  split_clients  "${remote_addr}"   $variant {

    10% .1;

    20% .2;

    30% .4; 40% .5;

     *  "";

    }

 

  server {

    listen       80;

    server_name  www.xx.com;

 

    access_log logs/custom.log custom;

 

location / {

      root   html;

      index index${variant}.html;

}

 

    ……

}

   

Nginxweb目录结构:   

 

[root@mail html]# tree -L 1

.

|-- member

|-- simg0

|-- upload

   ……

|-- files

|-- index.html

|-- index.1.html

|-- index.2.html

|-- index.3.html

|-- index.4.html

`-- index.5.html

 

                 

8 directories, 12 files

 

我们看监控一下Nginx的访问日志

 

[root@mail logs]# tail -f  custom.log

03/Sep/2011:12:15:41 +0800|www.xx.com|321|887|100.100.170.248|.4

03/Sep/2011:12:15:46 +0800|www.xx.com|319|887|192.168.3.248|.5

03/Sep/2011:12:16:00 +0800|www.xx.com|615|389|61.135.169.106|.2

03/Sep/2011:12:18:27 +0800|www.xx.com|565|389|23.123.123.128|.2

03/Sep/2011:12:19:26 +0800|www.xx.com|563|887|192.168.1.164|.4

03/Sep/2011:12:20:09 +0800|www.xx.com|563|887|192.168.4.88|.5

03/Sep/2011:12:24:11 +0800|www.xx.com|566|887|119.184.137.242|.5

03/Sep/2011:12:25:13 +0800|www.xx.com|566|887|173.242.125.196|.5

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27043155/viewspace-732986/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27043155/viewspace-732986/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值