linux运维进阶-varnish软件

(基于Red Hat rhel6.5)
一.Varnish简介
Varnish是一款高性能且开源的反向代理服务器和http加速器
挪威的最大的在线报纸 Verdens Gang(vg.no) 使用 3 台 Varnish 代替了原来的 12 台 Squid,性能比以前更好,这是 Varnish 最成功的应用案例。
Varnish流程图:
   
二.安装Varnish

    varnish的安装不是直接在yum仓库里安装,而是将rpm包从官网下载下来,
    在安装包的目录下,执行yum install * -y 就可以进行安装了。
    Varnish的rpm包:
    varnish-3.0.5-1.el6.x86_64.rpm  
    varnish-libs-3.0.5-1.el6.x86_64.rpm
 三.反向代理
 vim /etc/sysconfig/varnish     #进入此文件里,进行实验环境的配置
  66 VARNISH_LISTEN_PORT=80  改为80端口,varnish直接去监听80端口
 vim /etc/varnish/default.vcl
  7 backend default {
  8 .host = “172.25.50.2”;   #反向代理去取数据的地方
  9 .port = “80”;
  10 }
  

  添加缓存命中代码和轮循代码:
  定义多个不同域名站点的后端服务器
backend web1 {
         .host = "172.25.50.2";
         .port = "80";
 }
 backend web2 {
         .host = "172.25.50.3";
         .port = "80";
 }
 director lb round-robin {  
 #将多个后端聚合成一个名为lb的组,轮叫模式
   {  .backend = web1;  }
   {  .backend = web2;  }
 }
 #访问www.wrh.org域名,到web1,访问bbs.wrh.org到web2
sub vcl_recv { if (req.http.host ~ "^(www.)?wrh.org") { set req.http.host = "www.wrh.org"; set req.backend = lb; return (pass);
#pass将当前请求直接转发到后端服务器。pass通常只处理静态页面。varnish也只缓存静态数据
    } elsif (req.http.host ~ "^bbs.wrh.org") {
       set req.backend = web2;
       } else {
       error 404 "wrh cache";
       }
   }
 sub vcl_deliver {
    if (obj.hits > 0) {
     set resp.http.X-Cache = "HIT from wrh cache";
    }
    else {
     set resp.http.X-Cache = "MISS from wrh cache";
    }
    return (deliver);
 }

/etc/init.d/varnish reload#重载varnish服务配置
提示:到172.25.50.2上开启httpd服务,并且在/var/www/html目录下,创建index.html
(如果不改变配置文件的名称,必须要用index.html)
四.查看缓存命中情况
   (缓存120秒清除一次,在这120秒内,显示是击中的)
 curl -I bbs.wrh.org
 
五.手动清除缓存
 varnishadm ban.url .*$     #清除所有
 varnishadm ban.url /index.html    #清除 index.html 页面缓存
 varnishadm ban.url /admin/$       #清除 admin 目录缓存
六.apach主机配置
 在server2中(httpd):
 1.yum install -y httpd
   cd /var/www/html/
   vim index.html -->>www.wrh.org
   /etc/init.d/httpd restart
 在server3中(httpd):
 1.yum install -y httpd
   cd /var/www/html/
   vim index.html  -->>bbs.wrh.org
   /etc/init.d/httpd restart
 提示:在物理机上测试的时候,记得要在物理机上添加解析,vim /etc/hosts

七.测试结果
   轮循模式:

    

END
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值