天天看點

Varnish代替Squid

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 "Alvin.zeng 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" && 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域名的請求則傳回“Alvin.zeng 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

報錯方式二:

varnishd -n /usr/local/services/varnish/var/varnish/test-1/ -f /usr/local/services/varnish/vcl.conf -a 0.0.0.0:80 -s file,/opt/vcahce/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

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

8、檢視Varnish伺服器連接配接數與命中率:

/usr/local/varnish/bin/varnishstat

9、通過Varnish管理端口進行管理:

  用help看看可以使用哪些Varnish指令:

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help

說明其它:

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

但同時處理的請求數Varnish要比Squid高一些,,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