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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章