2013-11-05 08:20:01
来 源
itjs.cn
Varnish
本文介绍Varnish3.0安装配置详解,希望对于Varnish入门者给与帮助。更多Varnish资源请本站内搜索。

Varnish相对于squid,存在大量优势,经过生产环境实践,可以用其替换squid.

1. 安装

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

yum install varnish

如果安装过程中提供缺少lbiedit软件包,则使用以下命令安装libedit:

rpm -Uvh http://dev.centos.org/centos/5/testing/x86_64/RPMS/libedit-3.0-2.20090905cvs.el5.centos.x86_64.rpm

rpm -Uvh http://dev.centos.org/centos/5/testing/x86_64/RPMS/libedit-devel-3.0-2.20090905cvs.el5.centos.x86_64.rpm

下载范例配置

       cd /etc/varnish/

mv default.vcl default.vcl.bak

wget -O default.vcl http://yum.120ask.com/deploy/varnish-3.0/default.vcl

cd /etc/sysconfig

mv varnish varnish.bak

wget http://yum.120ask.com/deploy/varnish-3.0/varnish

2. 设置启动选项

chkconfig varnish on

chkconfig varnishlog off

chkconfig varnishncsa off

3. 启动服务

       service varnish restart

4. 测试

使用合适的测试工具,如curl, wget,甚至浏览器等测试服务是否正常。强烈建议重启机器以检查配置是否正常。

使用以上方法,即可快速安装好varnish 3.0, 安装完毕之后,一些重要的目录及文件需要特别熟悉:

文件

说明

/etc/varnish/default.vcl

服务配置文件,此文件至关重要。可下载现成的范例文件:http://yum.120ask.com/deploy/varnish-3.0/default.vcl

/etc/varnish/secret

varnishadm管理程序使用到的加密验证文件,此文件在启动varnish时会自动生成,无须建立

/etc/sysconfig/varnish

Varnishd守护进程启动参数配置文件(实质是一个shell脚本),请注意NFILES,MEMLOCK两个变量的值,可从以下地址下载范例文件:

http://yum.120ask.com/deploy/varnish-3.0/varnish

/usr/sbin/varnishd

Varnish的守护进程主程序,一般情况不需要直接运行之

/usr/bin/varnishadm

varnish的命令行管理工具

/usr/bin/varnishhist

以文本图形方式显示varnish的缓存命中分布状况

/usr/bin/varnishlog

显示varnish的shared memory logs, 在调试时候,可能会很有用

/usr/bin/varnishncsa

以NCSA格式记录varnish访问日志到文件系统

/usr/bin/varnish_reload_vcl

用于在运行时重新加载vcl配置,必须将/etc/sysconfig/varnish文件中的RELOAD_VCL设置为1

/usr/bin/varnishreplay

重现访问日志

/usr/bin/varnishsizes

与/usr/bin/varnishhist作用类似

/usr/bin/varnishstat

统计运行状态,如命中率、连接数、缓存条目等等。这个工具最为常用

/usr/bin/varnishtest

/usr/bin/varnishtop

显示varnish频率最高的活动状态

在这些文件中,/etc/varnish/default.vcl是最被频繁修改的文件,而/usr/bin/varnishstat则是在统计时使用最为频繁的工具。

Varnish的各项参数及配置,较为复杂,根据具体应用不同,一般来说,只需要修改/etc/varnish/default.vcl即可。

关于Varnish Cache

Varnish Cache是一个web加速软件,用作web服务加速的反向代理,与Squid不同的是它建立在较新的系统内核调用上,并且主要是使用内存作为缓存,它现有的使用者有facebook等,据使用者反馈,其与Squid相比,相同的访问量下连接数大大减少。

本人测试过程

准备一个普通的HTTP web服务器,我在虚拟机内启动了一个Linux+Apache+MySQL+Php环境,配置文件未改动,下载一个PHPWind 的bbs程序拿来测试。

在另外一个服务器上编译安装Varnish 3.0(IP:192.168.159.5),默认安装路径,安装过程可参考官方文档。

编辑Varnish的默认配置文件(/usr/local/etc/varnish/default.vcl):

#首先设置一个后端服务器

backend default {

.host = "192.168.159.11";

.port = "80";

}

sub vcl_recv {

if (req.restarts == 0) {

"code-keyword">if (req.http.x-forwarded- "code-keyword">for) {

set req.http.X-Forwarded-For =

req.http.X-Forwarded-For + ", " + client.ip;

} else {

"code-keyword">set req.http.X-Forwarded-For = client.ip;

}

}

#把除了以下这些类型请求以外的访问请求全部直接管道发送到后端的服务器

"code-keyword">if (req.request != "GET" &&

req.request != "HEAD" &&

req.request != "PUT" &&

req.request != "POST" &&

req.request != "TRACE" &&

req.request != "OPTIONS" &&

req.request != "DELETE") {

/* Non-RFC2616 or CONNECT which is weird. */

return (pipe);

}

#只有GET与HEAD方法才会使用Lookup,使用缓存。

"code-keyword">if (req.request != "GET" && req.request != "HEAD") {

/* We only deal  "code-keyword">with GET and HEAD by default */

return (pass);

}

#  "code-keyword">if (req.http.Authorization || req.http.Cookie) {

#     /* Not cacheable by default */

#     return (pass);

# }

#如果请求的是php页面直接转发到后端服务器

"code-keyword">if (req.url ~ ".(php|cgi)($|?)") {

return (pass);

}

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 == "*") {

/*

* Mark as "Hit-For-Pass"  "code-keyword">for the next 2 minutes

*/

set beresp.ttl = 120 s;

return (hit_for_pass);

}

"code-keyword">if (req.url ~ ".(png|gif|jpg)$") {

unset beresp.http.set-cookie;

set beresp.ttl = 1h;

}

#设置图片的缓存TTL为一小时

return (deliver);

}

sub vcl_deliver {

return (deliver);

}

sub vcl_error {

"code-keyword">set obj.http.Content-Type = "text/html; charset=utf-8";

set obj.http.Retry-After = "5";

synthetic {"

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>"} + obj.status + " " + obj.response + {"</title>

</head>

<body>

<h1>Error "} + obj.status + " " + obj.response + {"</h1>

<p>"} + obj.response + {"</p>

<h3>Guru Meditation:</h3>

<p>XID: "} + req.xid + {"</p>

<hr>

<p>Varnish cache server</p>

</body>

</html>

"};

return (deliver);

}

sub vcl_init {

return (ok);

}

sub vcl_fini {

return (ok);

}

#

添加Varnishd进程用户www,用户组www,创建/var/vcache目录,使www用户有权限可读写。

groupadd www

useradd www -g www

mkdir /var/vcache

chown -R www:www /var/vcache

chmod -R 750 /var/vcache

编辑/etc/sysctl.conf 优化几个内核参数:

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

运行sysctl -p 重新按配置文件设置内核参数。

启动Varnishd

varnishd -a 0.0.0.0:80 -f /usr/local/etc/varnish/ "code-keyword">default.vcl -T 127.0.0.1:2000 -s file,/var/vcache/,1G -u www

参数说明:-f指定了配置文件,-T是指定命令行管理界面监听地址,-s file指定了使用文件做缓存,1G是缓存文件大小,-u就是进程的用户了。

在客户端访问http://192.168.159.5/phpwind ,高频率刷新页面观察varnishd一端netstat -n输出,可以发现Varnish端到后端(apache)的TCP连接几乎一闪而过,很快就释放掉。

解决后端服务器不能日志记录真实访问者IP的问题,修改apache日志格式。

LogFormat  "code-quote">"%{X-Forwarded-For}i %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""  varnish_combined

之后修改Apache的虚拟主机日志格式或者默认日志格式为 varnish_combined.

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。