編譯安裝 varnish-4.1.2和yum 安裝 varnish-4.0.3


     vanish可以讓用戶自己選擇緩存數據是存於內存還是硬盤,存於內存一般基於二八法則即常訪問的數據是磁盤存儲的總數據五分之一,因此內存也應該是硬盤文件大概
五分之一。如果有多台vanish則,總內存滿足即可,由前端代理服務器實現負載調度。最后由於緩存需要平凡的刷新,因此內存也不要過大,否則會浪費在內存調度算法上。
一般7500轉每分的磁盤只能完成每秒80左右的隨機io,為了增加磁盤io,則可以做raind0,固態硬盤擦寫十萬次,是指寫滿整個磁盤才算一次。
      緩存的保持有兩種方式為文檔請求機制和時間過期機制,時間過期是指包含在客戶端頭部包括exprire(http1.0中)指緩存過期的絕對時間,cache-control(http1.1)

指緩存過期的相對時間。cache-control中有很多控制機制,其中用的最多的max-age,相對時間一般為具體值如200是,當expire和cache-control同時存在時,以cache-conrol

為中。

     Vanish緩存的保持有三種方法.file Malloc persistent等,前兩種在服務器重啟后緩

存數據會丟失。vanish系統配置文件為/etc/sysconfig/vanish

1.首先去官網下載

  wget http://repo.varnish-cache.org/source/varnish-4.1.2.tar.gz

2.解壓縮 安裝所需其他庫文件

   tar zxf  varnish-4.1.2.tar.gz

  編譯安裝過程可能會缺少以下包  libedit-devel  pcre-devel  ncurses-devel

  yum -y install libedit-devel 

  yum -y install pcre-devel 

  yum -y install ncurses-devel

  ./configure --prefix=/usr/local/varnish  \

        --bindir=/usr/local/varnish/bin   \

        --sbindir=/usr/local/varnish/sbin

3.安裝

 make && make install    

4.生成配置文件

 cp etc/example.vcl /usr/local/varnish/default.vcl

5.啟動

sbin/varnishd -f var/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80

6.使用yum安裝varnish-4.0.3 另外也可以yum安裝,由於varnish存在於epel源中因此

  yum install epel-release

  yum clean all

  yum list repo    更新並查看epel源是否已經存在
  rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el7.rpm
  yum install varnish

  yum 安裝后varnish的配置文件為/etc/varnish/varnish.params  其中定義了varnish的默認acl為/etc/varnish/default.acl

  systemctl start varnishd.service                                啟動varnish

  varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 登錄varnish的管理接口 默認的 varnish的管理端口為6082

 使用以下命令編譯修改的vcl

 再使用如下命令啟用編譯后的vcl

 默認的如果沒有default的backend 則會使用第一個定義的backend 否則會使用defaultbackend

 

 在使用varnishstat查看varnish的命中情況時 報錯

 

 使用ps -ef | grep varnishd 查看_.vsm文件所在

 

 

使用 lsof -p 3031 查看

 

創建varnishstat 所需的文件

 

 因此可以正常運行varnishstat了

 注意:緩存一般由鍵-值組成,當設定cookie后 緩存就會由鍵-值-cookies 組成 只有相同用戶請求相同資源才會命中,因此為了提高命中率。必須在vcl_recv中移除cookie,

且在vcl_fetch 中

acl 4.0 語法講解

vcl 4.0;                         //4.0定義必須聲明
import directors;           //4.0定義必須聲明
backend web1 {
  .host = "172.16.1.2";
  .port = "80";
 
}

backend web2 {
   .host = "172.16.1.3";
   .port = "80";
   .probe = {
     .url = "/index.html";
     .window = 5;
     .threshold = 2;
     .interval = 3s;
   }
}
sub vcl_init {  //3.0中無,4.0中在使用director做負載均衡時必須使用此指令
#  new round_robin_director = directors.round_robin();
   new random_director = directors.random();
   random_director.add_backend(web1, 10);
   random_director.add_backend(web2, 5);

}
#directors websrvs random {      //3.0中定義方式
#   {
#     .backend = web1;
#     .weight = 2;
#   }
#   {
#     .backend = web2;
#     .weight = 1;
#   }
#
#}

acl purgers {
 "127.0.0.1";
 "172.16.0.0"/16;
}


sub vcl_recv {
 
# set req.backend = webservs;     //3.0中定義方式
   set req.backend_hint = random_director.backend();
# if (req.url ~ "\.(jpg|png|css)$") {
#   set req.backend = web1;
# } else {
#
#   set req.backend = web2;
#}
 
 if (req.method == "PURGE") {
   if (!client.ip ~ purgers){
#   error is used 3.0,now is replaced by synth
#      error 405 "method not allow";
       return(synth(405,"method not allowed"));
   }
   return (purge);
 }

 if (req.url ~ "test.html") {  //若請求的為test.html則不查詢緩存

    return(pass);

  }
  return(hash);  //3.0中return(lookup),4.0中返回return(hash)而在vcl_hash中才返回lookup

}


#sub vcl_hit {     //3.0中定義方式,4.0中 purge 只能使用在vcl_recv中,且只能return
#  if (req.method == "PURGE"){
#      return (purge);
#    error 200 "Purged OK";  //3.0中定義方式,4.0中使用synth輸入錯誤信息
#     return(synth(200,"purged OK"));
#  }

#}


sub vcl_miss {
  if (req.method == "PURGE"){
#      purge;                               //4.0中purge只能用在vcl_recv中,且只能return(purge)
#      error  404 "not in cache";
       return(synth(404,"not in cache"));
   }

}


sub vcl_pass {
   if (req.method == "PURGE"){
#     error 502 "purged on a passed object";
      return(synth(502,"purged on a passed object"));
   }

}


#sub vcl_fetch {    vcl_feth is used in varnish 3.0, now 4.0 is replaced by vcl_backend_response
 sub vcl_backend_response {

  if (bereq.url ~ "\.(jpg|gif|png)$") {
    set beresp.ttl = 3600s;
  }
  if (bereq.url ~ "\.(css|js|html)$") {
    set beresp.ttl = 1200s;
  }

}

sub vcl_deliver{   //vcl_deliver即最終返回給用戶時修改response中的信息

 if (obj.hits > 0) {

    set resp.http.X-Cache = "hit from " + server.ip;
  } else {


    set resp.http.X-Cache = "miss";
  }

}

 HTTP緩存詳細查看

 http://www.cnblogs.com/_franky/archive/2011/11/23/2260109.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM