varnish的基本配置

#######varnish#########

1.安装

varnish-libs-3.0.5-1.el6.x86_64.rpm      
varnish-3.0.5-1.el6.x86_64.rpm
yum install * -y

2.配置varnish

vim /etc/sysconfig/varnish
 66 VARNISH_LISTEN_PORT=80  ##改监听端口为80

3.缓存命中

varnish存在的主机:
vim /etc/varnish/default.vcl  ##更改配置文件
  backend web1 {          ##配置一个后端服务器
    .host = "172.25.33.2";  ##对应web1的地址
    .port = "80";
  }

  backend web2 {        ##配置后端服务器
    .host = "172.25.33.3";  ##对应web2的地址
    .port = "80";
  }
  sub vcl_deliver {
        if (obj.hits > 0) {
         set resp.http.X-Cache = "HIT from westos cache";  ##命中的显示
         }
        else {
         set resp.http.X-Cache = "MISS from westos cache";  ##未命中的显示
         }
        return (deliver);
   }
/etc/init.d/varnish reload    ##重新加载配置文件
后端服务的机器只需开启httpd服务,进行访问测试

4.手动清除缓存

varnishadm ban.url .*$            #清除所有
varnishadm ban.url /index.html        #清除 index.html 页面缓存

varnishadm ban.url /admin/$        #清除 admin 目录缓存

varnish的基本配置


5.访问不同的域名,从不同的主机拿取数据

vim /etc/varnish/default.vcl  ##更改配置文件
  backend web1 {          ##配置一个后端服务器
    .host = "172.25.33.2";  ##对应web1的地址
    .port = "80";
  }

  backend web2 {        ##配置后端服务器
    .host = "172.25.33.3";  ##对应web2的地址
    .port = "80";
  }
 sub vcl_recv {
 if (req.http.host ~ "^(www.)?westos.org") {
       set req.http.host = "www.westos.org";        ##域名为www.westos.com或westos.com
       set req.backend = web1;                ##到达web1
       } elsif (req.http.host ~ "^bbs.westos.org") {    ##域名为bbs.westos.org
        set req.backend = web2;                ##到达web2
       } else {error 404 "westos cache";        ##显示error 404
     }
}
不同的主机测试页面不同,显示结果也不同

6.负载均衡

vim /etc/varnish/default.vcl  ##更改配置文件
  backend web1 {          ##配置一个后端服务器
    .host = "172.25.33.2";  ##对应web1的地址
    .port = "80";
  }

  backend web2 {        ##配置后端服务器
    .host = "172.25.33.3";  ##对应web2的地址
    .port = "80";
  }
  director lb round-robin {  ##定义负载均衡器lb
      { .backend = web1; }
      { .backend = web2; }
  }

  sub vcl_recv {
    if (req.http.host ~ "^(www.)?westos.org") {
        set req.http.host = "www.westos.org";  ##主机名为www.westos.com或westos.com
        set req.backend = lb;                   ##到达lb
    return (pass);                ##清楚缓存
           }
        elsif (req.http.host ~ "^bbs.westos.org") {
        set req.backend = web1;                 ##到达web1
       } else {error 404 "westos cache";
          }
      }  
}
在server2主机上建立虚拟主机,后进行测试

vim /etc/httpd/conf/httpd.conf

varnish的基本配置

7.varnish推送平台

yum install unzip -y
yum install php -y
unzip bansys.zip -d /var/www/html/    ##解压文件
vim /etc/httpd/conf/httpd.conf      ##更改http配置文件
  Listen:8080    ##监听端口改为8080
vim /var/www/html/config.php
  <?php
   //varnish主机列表
   //可定义多个主机列表
   $var_group1 = array(
                        'host' => array('172.25.33.1',),       ##varnish主机
                                                'port' => '80',    ##对应段口号
                    );




   //varnish群组定义
   //对主机列表进行绑定
   $VAR_CLUSTER = array(
                         'www.westos.org' => $var_group1,    ##对应域的主机
                     );


   //varnish版本
   //2.x和3.x推送命令不一样
   $VAR_VERSION = "3";

  ?>
vim /etc/varnish/default.vcl
  acl westos {
   "127.0.0.1";
   "172.25.33.0"/24;    ##接受推送的网段
  }
  sub vcl_recv {
    if (req.request == "BAN") {
    if (!client.ip ~ westos) {
      error 405 "Not allowed.";
   }
    ban("req.url ~ " + req.url);
    error 200 "ban added";
 }
}
/etc/init.d/varnish reload    ##重新加载配置文件
测试:
    http://172.25.33.1:8080            ##显示推送页面

varnish的基本配置

varnish的基本配置
    http://www.westos.org/index.html    ##显示结果页面