用来代替SQUID的软件VARNISH

简介:

搭建性能比squid高很多的varnish服务器

[文章作者:jackbillow  转载请注明出处:http://www.discuz.net]

arnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang (http://www.vg.no) 使用3台Varnish代替了原来的12台squid,

性能比以前更好。
varnish的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两

种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此squid 

cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,

这就是 Varnish cache设计架构。


1.下载源码包编译安装:

cd /usr/local/src && wget http://nchc.dl.sourceforge.net/s ... arnish-1.1.1.tar.gz
tar zxvf /usr/local/src/varnish-1.1.1.tar.gz
cd /usr/local/src/varnish-1.1.1
./autogen.sh
./configure --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
注:如果你的gcc版本是4.2.0或更高的版本,可以加上--enable-extra-warnings编译参数,在出错时,得到附加的警告信息。
我这里是用源码包安装的,如果你是redhat或centos可以用rpm包来安装(rpm下载位置:http: //sourceforge.net/project/showfiles.php?

group_id=155816&package_id=173643&release_id=533569).



2. 建立cache目录:
mkdir -p /cache/varnish/V  && chown -R nobody:nobody /cache



3.编写启动文件:

cd /usr/local/varnish/sbin
vi start.sh
内容如下:
#!/bin/sh
# file: go.sh
date -u
/usr/local/varnish/sbin/varnishd \
-a 10.0.0.129:80 \
-s file,/cache/varnish/V,1024m \
-f /usr/local/varnish/sbin/vg.vcl.default \
-p thread_pool_max=1500 \
-p thread_pools=5 \
-p listen_depth=512 \
-p client_http11=on \
注:-a 是指定后端服务器的ip或hostname,就象squid做revese proxy时的originserver.
不过这个也可以在vcl里面写。
-f 是指定所用的vcl的文件。
-s 指定cache目录的存储类型,文件位置和大小。
-p 是指定varnish的启动的一些启动参数,可以根据自己的机器配置来优化varnish的性能。
其他参数已经参数的具体含义可以用varnishd --help 来查看。



4.编写vcl:
我的vcl如下:

backend default {
        set backend.host = "127.0.0.1";
        set backend.port = "http";
}

#我用的是一台机器做测试,使用的backend用的是127.0.0.1:80.如果varnish机器和后台的机器分开的。
写上对应的机器的ip或hostname就可以了。

sub vcl_recv {

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

        if (req.http.Expect) {
                pipe;
        }

        if (req.http.Authenticate || req.http.Cookie) {
                pass;
        }

        if (req.request == "GET" && req.url ~ "\.(gif|jpg|swf|css|js)$") {
                lookup;
        }
                lookup;
        }


sub vcl_pipe {
        pipe;
        }


sub vcl_pass {
        pass;
        }

sub vcl_hash {
             hash;
         }

sub vcl_hit {
        if (!obj.cacheable) {
                pass;
        }
                deliver;
        }

sub vcl_timeout {
        discard;
}

sub vcl_discard {
        discard;
}


如果是多个站点在不同的originserver时,可以使用下面配置:

backend www {
       set backend.host = "www.jackbillow.com";
       set backend.port = "80";
         }

backend images {
      set backend.host = "images.jackbillow.com";
      set backend.port = "80";
         }

sub vcl_recv {
      if (req.http.host ~ "^(www.)?jackbillow.com$") {
            set req.http.host = "www.jackbillow.com";
            set req.backend = www;
      } elsif (req.http.host ~ "^images.jackbillow.com$") {
            set req.backend = images;
      } else {
            error 404 "Unknown virtual host";
}



5.启动varnish:

/usr/local/varnish/sbin/start.sh

Mon Sep  3 03:13:19 UTC 2007
file /cache/varnish/V/varnish.tEKXXx (unlinked) size 1073741824 bytes (262144 fs-blocks, 262144 pages)
Using old SHMFILE

ps waux | grep varnish 
root     16254  0.0  0.0 11200  708 ? Ss   10:43   0:00 /usr/local/varnish/sbin/varnishd -a 10.0.0.129:80 -s /varnish/V,1024m 

-f /usr/local/varnish/sbin/vg.vcl.default -p thread_pool_max 1500 -p thread_pools 5 -p listen_depth 512 -p client_http11 on
nobody   16255  0.0  0.1 1152552 1808 ?      Sl   10:43   0:00 /usr/local/varnish/sbin/varnishd -a 10.0.0.129:80 -s 

file,/cache/varnish/V,1024m -f /usr/local/varnish/sbin/vg.vcl.default -p thread_pool_max 1500 -p thread_pools 5 -p 

listen_depth 512 -p client_http11 on

看到上面信息说明varnish正确启动,恭喜你,你已经配置成功了。:)









本文转自 jxwpx 51CTO博客,原文链接:http://blog.51cto.com/jxwpx/216878,如需转载请自行联系原作者
目录
相关文章
Squid 安装与配置
本文转载自:https://help.aliyun.com/knowledge_detail/41342.html Squid 介绍 Squid 是一个缓存 Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据。
1498 0
|
Web App开发 开发工具
|
存储 缓存 文件存储
|
缓存 前端开发 JavaScript