緩存服務varnish安裝配置

一、varnish優點
1、數據存儲,可以使用內存緩存,也可以使用磁盤大文件存儲,減少小文件頻繁讀寫
2、充分利用多核cpu,支持http/1.1協議
3、可以使用正則匹配緩存,配置靈活,思路清晰,管理端口,工具工具使用方便

二、工作原理流程

 

三、安裝及其配置

官方地址https://www.varnish-cache.org/
tar varnish-3.0.0.tar.gz
cd varnish-3.0.0
./configure --prefix=/usr/local/varnish3.0
make && make install

配置實例及說明

vim /usr/local/varnish3.0/etc/varnish/default.vcl

#設置後端服務
backend test1 {
    .host = "192.168.100.5";
    .port = "80";
    .connect_timeout = 1s;
    .first_byte_timeout = 5s;
    .between_bytes_timeout = 2s;
}
backend test2 {
    .host = "192.168.100.6";
    .port = "80";
    .connect_timeout = 1s;
    .first_byte_timeout = 5s;
    .between_bytes_timeout = 2s;
}
#定義負載均衡
director lb_test random {
    {
      .backend = test1;
      .weight = 5;
     }
     {
       .backend = test2;
       .weight = 5;
     }

}
#定義訪問控制列表
acl purge {
    "localhost";
    "127.0.0.1";
    "192.168.100.0"/24;
    "192.168.0.0"/24;
}


sub vcl_recv {
#開啓壓縮模式,圖片格式取消壓縮
if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {
        remove req.http.Accept-Encoding;
        remove req.http.Cookie;
    } else if (req.http.Accept-Encoding ~ "gzip") {
        set req.http.Accept-Encoding = "gzip";
    } else if (req.http.Accept-Encoding ~ "deflate") {
        set req.http.Accept-Encoding = "deflate";
    } else {
        remove req.http.Accept-Encoding;
    }
}
#根據host設置後端服務器
  if (req.http.Host ~ "(?i)(www.test1.com|www.test2.com)") {
     set req.backend = lb_test;
   }else
  if (req.http.Host ~ "(?i)p_w_picpath.wdj.com"){
     set req.backend = test1;
  }else
  {
    error 408 "Hostname not found";
  } 
#如果爲purge請求,客戶端ip不在訪問列表中,返回405拒絕
  if (req.request == "PURGE") {
     if (!client.ip ~purge) {
       error 405 "Not Allowed";
   }
#本地緩存查找
   return(lookup);
  }
#如果爲GET請求,url後綴爲jpg,png,gif等 取出cookie
  if (req.request == "GET"&&req.url ~ "(?i)\.(jpg|png|gif|swf|jpeg|ico)$") {
        unset req.http.cookie;
  }
#如果GET請求,url爲php,則穿過cache,不緩存
  if (req.request =="GET"&&req.url ~ "(?i)\.php($|\?)"){
        return (pass);
  }
#簡單防盜鏈
if (req.http.referer ~ "http://.*") {
  if ( !(req.http.referer ~ "http://.*test1\.com"
     || req.http.referer ~ "http://.*test2\.com"
     || req.http.referer ~ "http://.*wdj\.com"
     || req.http.referer ~ "http://.*google\.com"
     || req.http.referer ~ "http://.*baidu\.com"
     || req.http.referer ~ "http://.*yahoo\.cn"
  )) {
      error 404 "Not Found!";
 }
}
#獲取客戶端ip
#     if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For =
                req.http.X-Forwarded-For + ", " + client.ip;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
#   }
#不是以下請求進入pipe模塊
    if (req.request != "GET" &&
      req.request != "HEAD" &&
      req.request != "PUT" &&
      req.request != "POST" &&
      req.request != "TRACE" &&
      req.request != "OPTIONS" &&
      req.request != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }
#不是GET 和HEAD請求不緩存
    if (req.request != "GET" && req.request != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization) {
        /* Not cacheable by default */
        return (pass);
    }
    return (lookup);
}
#
 sub vcl_pipe {
     return (pipe);
 }
#
sub vcl_pass {
    return (pass);
}
#使用url+host hash算法查找數據
sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (hash);
}
# 如果請求爲purge 將清除緩存
sub vcl_hit {
   if (req.request == "PURGE") {
       set obj.ttl = 0s;
       error 200 "Purged";
    }
    return (deliver);
}

sub vcl_miss {
    return (fetch);
}
#
sub vcl_fetch {
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 0 s;
                return (hit_for_pass);
    }
    if (beresp.http.Pragma ~"no-cache" ||
    beresp.http.Cache-Control ~"no-cache" ||
    beresp.http.Cache-Control ~"private") {
      return (deliver);
   }
#爲特定格式文件設置緩存時間
    if (req.request == "GET"&&req.url ~ "(?i)\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$") {
    set beresp.ttl = 30d;
  }
   if (req.request == "GET"&&req.url ~ "(?i)\.(html|htm)$") {
    set beresp.ttl = 1d;
  }
    return (deliver);
}
# 設置返回狀態
 sub vcl_deliver {
     set resp.http.x-hits = obj.hits;
     if (obj.hits > 0) {
      set resp.http.X-Cache = "Hit test.com";
   }else {
       set resp.http.X-Cache = "Miss test.com";
   }
     set resp.http.Server = "BWM";
     return (deliver);
 }
# 定義錯誤
sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    set obj.http.Retry-After = "5";
    synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>"} + obj.status + " " + obj.response + {"</title>
  </head>
  <body>
    <h1>Error "} + obj.status + " " + obj.response + {"</h1>
    <p>"} + obj.response + {"</p>
    <h3>Guru Meditation:</h3>
    <p>XID: "} + req.xid + {"</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>
"};
    return (deliver);
}

sub vcl_init {
        return (ok);
}

sub vcl_fini {
        return (ok);
}
四、啓動
啓動命令
/usr/local/varnish3.0/sbin/varnishd  -f /usr/local/varnish3.0/etc/varnish/default.vcl  -s malloc,2G  -a 0.0.0.0:80 -w 1024,51200,10 -t
3600 -T 192.168.100.2:3500
檢測配置文件是否存在錯誤
/usr/local/varnish3.0/sbin/varnishd -C -f /usr/local/varnish3.0/etc/varnish/default.vcl
參數
-a  address:port 監聽端口
-f  指定配置文件
-s 指定緩存類型 malloc爲內存, file 文件緩存
-t 默認TTL
-T address:port 管理端口
-w 最小線程,最大線程,超時時間


記錄varnish日誌
 /usr/local/varnish3.0/bin/varnishncsa -w /var/logs/varnish.log &

五、緩存刷新

php腳步如下
<?php  
function purge($ip, $url)  
{  
    $errstr = '';  
    $errno = '';  
    $fp = fsockopen ($ip, 80, $errno, $errstr, 2);  
    if (!$fp)  
    {  
         return false;  
    }  
    else 
    {  
        $out = "PURGE $url HTTP/1.1\r\n";  
        $out .= "Host:p_w_picpath.wdj.com\r\n";  
        $out .= "Connection: close\r\n\r\n";  
        fputs ($fp, $out);  
        $out = fgets($fp , 4096);  
        fclose ($fp);  
         return true;  
    }  
}  
if(isset($_POST['content'])) {
//$str=str_replace("\r","<br>",$_POST[content]);
$arr=(explode("\n",$_POST['content']));
$Server="192.168.100.2";
foreach ($arr as $value)
{
  echo $value;
  purge($Server, $value);  
  echo "刷新成功"."<br />";
}
//purge($Server, "$_POST[content]");  
//echo "刷新成功";
}
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>varnish緩存刷新</title>
</head>

<body>

<form action="?" method="post">
填寫需要刷新url<br />
<textarea name="content" cols="40" rows="10"></textarea>
<input type="submit" value="submit" />
</form>
</body>
</html>

###########
通過管理端口清除緩存
telnet 192.168.100.2 3500
3.0版本 爲ban.url aaaa.html
2.版本爲purge.url aaaa.html

vcl.load平滑切換配置文件
vcl.load 配置文件名   文件路徑
如vcl.load 123  /usr/local/varnish3.0/etc/varnish/default.vcl
 

 

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