当前位置: 首页 > 工具软件 > varnish-agent > 使用案例 >

RHEL6.4安装varnish

鲜于岳
2023-12-01


varnish开源项目是2006年发布的第一个版本0.9.距今已经八年多了,此文档之前也提过varnish还不稳定,那是2007年时候编写的,现在的varnish已经很健壮。很多门户网站已经部署了varnish,并且反应都很好,甚至反应比squid还稳定,且效率更高,资源占用更少。相信在反向代理,web加速方面,varnish已经有足够能力代替squid。


Varnish 的作者Poul-Henning Kamp是FreeBSD(BSD UNIX分支的开发者)的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。


一个典型的例子:

挪威大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好


下边进行varnish的安装

varnish官网:https://www.varnish-cache.org

varnish下载:https://www.varnish-cache.org/releases


varnish支持的平台(操作系统)有:

此处以RHEL6为准测试

varnish已经对RHEL&CentOS支持很好,支持在线yum安装命令如下:

#rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release-3.0-1.el6.noarch.rpm

rpm直接远程安装这个包会生成yum源的配置文件

#yum install varnish #即可安装varnish chache server

此处我的机器不能连接到互联网,所有采用源码包安装

源码包下载地址:http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz

RPM包下载地址:http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/


下边先说源码包的安装:

[root@master ~]# cd /usr/src/ && tar -zxvf varnish-3.0.3.tar.gz && cd varnish-3.0.3       #将下载的包mv到 /usr/src然后解包再进入varnish的安装目录

[root@master varnish-3.0.3]# yum -y install gcc glib gcc-c++ pcre-devel       #安装依赖包为了兼容正则表达式

[root@master varnish-3.0.3]# ./configure --prefix=/usr/local/varnish       #编译安装的配置

[root@master varnish-3.0.3]# make && make install                                #编译和安装

至此安装完成,下边简单叙述一下rpm包的安装过程:

获取包

# wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-agent-1.16-0.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-debuginfo-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-docs-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-libs-3.0.4-1.el6.x86_64.rpm && wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/varnish-libs-devel-3.0.4-1.el6.x86_64.rpm

以上包包含动态链接库包,文档包,开发依赖包等等

[root@slave RPMvarnish]# rpm -ivh varnish-debuginfo-3.0.4-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:varnish-debuginfo      ########################################### [100%]
[root@slave RPMvarnish]# rpm -ivh varnish-docs-3.0.4-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:varnish-docs           ########################################### [100%]
[root@slave RPMvarnish]# rpm -ivh varnish-libs-3.0.4-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:varnish-libs           ########################################### [100%]
[root@slave RPMvarnish]# rpm -ivh varnish-libs-devel-3.0.4-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:varnish-libs-devel     ########################################### [100%]
[root@slave RPMvarnish]#varnish-agent-1.16-0.el6.x86_64.rpm

Preparing...                ########################################### [100%]
   1:varnish-libs-devel     ########################################### [100%]

如果支持连接到网络的话建议用如下方式安装:

For RHEL 6 and compatible distributions, use

     rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release-3.0-1.el6.noarch.rpm


and then run

yum install varnish

The --no-signature is only needed on initial installation, since the Varnish GPG key is not yet in the yum keyring

下边进行varnish的启动介绍和简单的配置:

简单的配置varnish:

[root@master varnish-3.0.3]# cat /etc/varnish/default.vcl
backend webserver {
     .host = "127.0.0.1";                          //后端服务器的IP地址或者完全的FQDN名或者hostname
     .port = "8000";                                       //后端服务器提供服务的端口
 }

sub vcl_recv {
        if (!req.http.x-forwarded-for) {
                set req.http.X-Forwarded-For = client.ip;
        }
            if (req.request != "GET" &&
               req.request != "HEAD" &&
               req.request != "PUT" &&
               req.request != "POST" &&
               req.request != "TRACE" &&
               req.request != "OPTIONS" &&
               req.request != "DELETE") {
                   return (pipe);
            }
       if (req.request != "GET" && req.request != "HEAD") {
               return (pass);
        }
        if (req.url ~ "\.(jsp|do)($|\?)") {
            return (pass);
        }
    else {
            return (lookup);
        }
}
 
sub vcl_pipe {
        return (pipe);
}
 
sub vcl_pass {
        return (pass);
}
 
sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (hash);
}
 
sub vcl_hit {
            return (deliver);
}
 
sub vcl_miss {
        return (fetch);
}
 
sub vcl_fetch {
     if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
               set beresp.ttl = 120 s;
               return (hit_for_pass);
     }
    if (req.request == "GET" && req.url ~ "^/static(.*)$") {
       set beresp.ttl = 600s;
    }
    if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {
       set beresp.ttl = 1800s;
    }
    return (deliver);
}
sub vcl_deliver {
        if (obj.hits > 0) {
               set resp.http.X-Cache = "HIT john.local";
        }
    else {
              set resp.http.X-Cache = "MISS from john.local";
        }
        return (deliver);
}

****************************************

这是一个简单的示例性配置文件笔者

没有作出详细的注释或者解释,笔者

将会在后边的博文中渐渐解释VCL也

就是varnish的配置文件的写法

****************************************

启动varnish服务:

[root@master varnish-3.0.3]# /usr/local/varnish/sbin/varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80

参数说明:

f: 需要加载的vcl文件

s:用malloc函数动态调度内存,为1G

T:管理端口和监听地址

a:服务的监听端口,对外提同服务


简单的测试:

[root@master varnish-3.0.3]# lsof -i:80
COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
varnishd 13467 nobody    6u  IPv4  25286      0t0  TCP *:http (LISTEN)


[root@master varnish-3.0.3]# curl -I http://192.168.0.111
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sun, 11 Aug 2013 11:57:24 GMT
ETag: "248c-1b-4e3aab609ddb4"
Content-Type: text/html; charset=UTF-8
Content-Length: 27
Accept-Ranges: bytes
Date: Sat, 31 Aug 2013 15:00:45 GMT
X-Varnish: 2074431089 2074431086
Age: 48
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT john.local                        //HIT为命中缓存

[root@master ~]# curl -I http://192.168.0.111
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sun, 11 Aug 2013 11:57:24 GMT
ETag: "248c-1b-4e3aab609ddb4"
Content-Type: text/html; charset=UTF-8
Content-Length: 27
Accept-Ranges: bytes
Date: Sat, 31 Aug 2013 15:02:43 GMT
X-Varnish: 2074431090
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from john.local                  //MISS为未命中


VCL写法将会在下篇博文中详细剖析

VCL详解 请移步至:

http://blog.csdn.net/cwg_1992/article/details/10742625

########################################

本文有笔者原创

作者:john

转载请注明出处

 类似资料: