使用Varnish代替Squid做網站緩存加速器的詳細解決方案

1、Varnish採用了“Visual Page Cache”技術,在內存的利用上,Varnish比Squid具有優勢,它避免了Squid頻繁在內存、磁盤中交換文件,性能要比Squid高。
  2、Varnish的穩定性還不錯,我管理的一臺圖片服務器運行Varnish已經有一個月,沒有發生過故障,而進行相同工作的Squid服務器就倒過幾次。
  3、通過Varnish管理端口,可以使用正則表達式快速、批量地清除部分緩存,這一點是Squid不能具備的。
  
--------------------------------------------------------------------------------
  下面來安裝Varnish網站緩存加速器(Linux系統):
  1、創建www用戶和組,以及Varnish緩存文件存放目錄(/var/vcache):
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache

  2、創建Varnish日誌目錄(/var/logs/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs

  3、編譯安裝varnish:
wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install

  4、創建Varnish配置文件:
vi /usr/local/varnish/vcl.conf
  輸入以下內容:

backend myblogserver {
       set backend.host = "192.168.0.5";
       set backend.port = "80";
}
acl purge {
       "localhost";
       "127.0.0.1";
       "192.168.1.0"/24;
}
sub vcl_recv {
       if (req.request == "PURGE") {
               if (!client.ip ~ purge) {
                       error 405 "Not allowed.";
               }
               lookup;
       }
       if (req.http.host ~ "^blog.s135.com") {
               set req.backend = myblogserver;
               if (req.request != "GET" && req.request != "HEAD") {
                       pipe;
               }
               else {
                       lookup;
               }
       }
       else {
               error 404 "Zhang Yan Cache Server";
               lookup;
       }
}
sub vcl_hit {
       if (req.request == "PURGE") {
               set obj.ttl = 0s;
               error 200 "Purged.";
       }
}
sub vcl_miss {
       if (req.request == "PURGE") {
               error 404 "Not in cache.";
       }
}
sub vcl_fetch {
       if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
               set obj.ttl = 3600s;
       }
       else {
               set obj.ttl = 30d;
       }
}
  這裏,我對這段配置文件解釋一下:
  (1)、Varnish通過反向代理請求後端IP爲192.168.0.5,端口爲80的web服務器;
  (2)、Varnish允許localhost、127.0.0.1、192.168.0.***三個來源IP通過PURGE方法清除緩存;
  (3)、Varnish對域名爲blog.s135.com的請求進行處理,非blog.s135.com域名的請求則返回“Zhang Yan Cache Server”;
  (4)、Varnish對HTTP協議中的GET、HEAD請求進行緩存,對POST請求透過,讓其直接訪問後端Web服務器。之所以這樣配置,是因爲POST請求一般是發送數據給服務器的,需要服務器接收、處理,所以不緩存;
  (5)、Varnish對以.txt和.js結尾的URL緩存時間設置1小時,對其他的URL緩存時間設置爲30天。
  5、啓動Varnish
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on

  6、啓動varnishncsa用來將Varnish訪問日誌寫入日誌文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &

  7、配置開機自動啓動Varnish
vi /etc/rc.local
  在末尾增加以下內容:

ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &

  8、優化Linux內核參數
vi /etc/sysctl.conf
  在末尾增加以下內容:

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

--------------------------------------------------------------------------------
  再看看如何管理Varnish:
  1、查看Varnish服務器連接數與命中率:
/usr/local/varnish/bin/varnishstat
  
  2、通過Varnish管理端口進行管理:
  用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help
 
Available commands:
ping [timestamp]
status
start
stop
stats
vcl.load
vcl.inline
vcl.use
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool

  3、通過Varnish管理端口,使用正則表達式批量清除緩存:
  (1)、例:清除類似http://blog.s135.com/a/zhangyan.html的URL地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/
  (2)、例:清除類似http://blog.s135.com/tech的URL地址:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$
  (3)、例:清除所有緩存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$

  4、一個清除Squid緩存的PHP函數(清除Varnish緩存同樣可以使用該函數,無需作任何修改,十分方便):
view plaincopy to clipboardprint?
<?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:blog.s135.com\r\n";  
        $out .= "Connection: close\r\n\r\n";  
        fputs ($fp, $out);  
        $out = fgets($fp , 4096);  
        fclose ($fp);  
        return true;  
    }  
}  
 
purge("192.168.0.4", "/index.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:blog.s135.com\r\n";
        $out .= "Connection: close\r\n\r\n";
        fputs ($fp, $out);
        $out = fgets($fp , 4096);
        fclose ($fp);
        return true;
    }
}
purge("192.168.0.4", "/index.php");
?>
  附1:Varnish官方網站:http://www.varnish-cache.org/
  附2:2007年12月10日,我寫了一個每天0點運行,按天切割Varnish日誌,生成一個壓縮文件,同時刪除上個月舊日誌的腳本(/var/logs/cutlog.sh):
  /var/logs/cutlog.sh文件內容如下:
#!/bin/sh
# This file run at 00:00
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz
  設置在每天00:00定時執行:
  
/usr/bin/crontab -e
  或者  
vi /var/spool/cron/root
  輸入以下內容:
引用
0 0 * * * /bin/sh /var/logs/cutlog.sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章