ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Nginx基于客户端请求头的访问分类

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

原创 Linux操作系统 作者:nginx_web 时间:2012-06-15 22:22:25 0 删除 编辑

 

 

   Nginx从0.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 

 

    access_log logs/custom.log custom;

 

location / {

      root   html;

      index index${variant}.html;

}

 

    ……

}

   

Nginx的web目录结构:   

 

[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||321|887|100.100.170.248|.4

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

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

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

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

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

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

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

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

下一篇: Nginx的cookie
请登录后发表评论 登录
全部评论

注册时间:2012-06-06

  • 博文量
    52
  • 访问量
    444539