天天看點

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

<a href="http://blog.s135.com/post/313.htm">http://blog.s135.com/post/313.htm</a>

  今天寫的這篇關于Varnish的文章,已經是一篇可以完全替代Squid做網站緩存加速器的詳細解決方案了。網上關于Varnish的資料很少,中文資料更是微乎其微,希望本文能夠吸引更多的人研究、使用Varnish。

  在我看來,使用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:

tar zxvf varnish-1.1.2.tar.gz

cd varnish-1.1.2

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

make &amp;&amp; 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" &amp;&amp; req.request != "HEAD") {

                       pipe;

               else {

                       lookup;

       else {

               error 404 "Zhang Yan Cache Server";

sub vcl_hit {

               set obj.ttl = 0s;

               error 200 "Purged.";

sub vcl_miss {

               error 404 "Not in cache.";

sub vcl_fetch {

       if (req.request == "GET" &amp;&amp; req.url ~ "/.(txt|js)$") {

               set obj.ttl = 3600s;

               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 &amp;

  7、配置開機自動啟動Varnish

vi /etc/rc.local

  在末尾增加以下内容:

/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &amp;

  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

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

  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管理端口,使用正規表達式批量清除緩存:

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/

/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緩存同樣可以使用該函數,無需作任何修改,十分友善):

&lt;?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");   

?&gt;  

&lt;?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");

?&gt;

  附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

mkdir -p /var/logs/youvideo/

gzip -c /var/logs/${date}.log &gt; /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

?PHP function&gt;

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

引用位址:

注意: 該位址僅在今日23:59:59之前有效

<a></a>

Jacky

2007-12-3 10:28

varnish如何做到在不重新開機的情況下重新載入配置檔案

2007-12-3 10:29

用varnish做反向代理的時候,登入一般的網站沒有任何問題。登入DZ論壇的背景出現無法登入,沒有任何提示。用squid就沒有這樣的問題,不知道那位老大遇到過這樣的問題。

出問題是肯定的,因為本文中的Varnish配置将緩存所有類型的檔案,而你使用squid之是以正常,是因為在squid配置檔案中沒有配置去緩存php檔案。同樣,對于Varnish,你可以選擇不緩存.php檔案,修改vcl.conf配置檔案:

if (req.request != "GET" &amp;&amp; req.request != "HEAD") {

  pipe;

elseif(req.url ~ "/.(php|cgi)($|/?)") {

  pass;

else {

  lookup;

itsea

2007-12-4 23:42

張兄,今天測了一下varnish,原來用nginx+squid ESTAB連接配接大概在1700的機器換了varnish後連接配接數隻有300多了,通路起來一切正常,cacti檢視到流量也是正常的,難道varnish就強在這

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

?配置是按您的配置做的。

另外還有個問題vcache這個目錄是用來儲存cache檔案的是嗎?我在ll vcache裡什麼檔案都沒有。

還有用varnish這個方法還解決了一直讓我頭疼的squid不支援iis compress問題,實在太感謝了

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

TCP連接配接數Varnish要比Squid少,因為Varnish的TCP連接配接釋放要比Squid快。

但同時處理的請求數Varnish要比Squid高一些,這是我在F5 BIG-IP下的兩台伺服器,一台Varnish、另一台Squid,F5 BIG-IP分給它們的連接配接數相同,Varnish實時處理的請求數比Squid多1倍,平均處理的請求數也比Squid多100餘個:

/usr/local/webserver/varnish/bin/varnishstat

-----------------------------------------------------------

   70979868       580.97       356.55 Client requests received

   70897998       580.97       356.14 Cache hits

/usr/local/squid/bin/squidclient -p 80 mgr:5min

client_http.requests = 248.425264/sec

client_http.hits = 245.135282/sec

如果正常的話,vcache這個目錄裡隻有一個大小為1G的檔案:varnish_cache.data

2007-12-5 10:06

今天遇到的問題貌似跟樓上說的一樣,varnish做反向代理後dvbbs登陸不正常,使用者登陸後顯示同一個使用者名,驗證碼不變。

同理,有些URL是實時的,不需要被緩存,可以自己修改配置檔案,用req.url ~過濾掉這些URL。

不過,不建議對discus!、dvbbs等别人寫的論壇程式使用Squid、Varnish做緩存,因為這些程式本身就沒有用PURGE指令去重新整理Squid、Varnish緩存的功能。

<a href="http://www.discuz.net/" target="_blank">http://www.discuz.net/</a>

Via  1.0 www1.discuz.net:80 (squid)

X-Cache  MISS from www1.discuz.net

X-Powered-By  PHP/5.2.4

<a href="http://www.discuz.net/register.php" target="_blank">http://www.discuz.net/register.php</a>

<a href="http://www.discuz.net/forumdata/cache/style_1.css" target="_blank">http://www.discuz.net/forumdata/cache/style_1.css</a>

X-Cache  HIT from www1.discuz.net

<a href="http://www.discuz.net/images/default/logo.gif" target="_blank">http://www.discuz.net/images/default/logo.gif</a>

minuteman

2007-12-6 16:20

正好這兩天我在自己的BLOG上也做了varnish緩存的實驗

用varnishncsa輸出日志,但發現裡面有不少日志條目是127.0.0.1來的通路,覺得比較奇怪,是不是varnishd工作時候産生的?上網找也沒找到相關的解釋。

老大你有沒有相關知識分享一下?嘿嘿

leftleg

2007-12-7 12:50

discuz 做緩存的話 ,可以試試使用緩存文章分頁。

2007-12-7 14:12

varnish可以實作類似于squid那樣的父子節點關系嗎?

2007-12-7 14:51

今天在另外一個平台上使用varnish測試

增加了

                   pipe;

                 }

              elseif(req.url ~ "/.(aspx|asp|shtml|vimg)($|/?)") {

                   pass;

              else {

                   lookup;

後還是偶爾有使用者登陸後顯示别人的使用者名

而且程式員更新js檔案後不能馬上看到

purge所有後也不行,還發現一個小問題執行 varnishadm後容易使varnishd父程序吃cpu 100%一直下不來

我的通路量在 350 request/sec

e文的了解能力比較查在man跟官方faq裡似乎沒看到類似情況