您的位置:首页 > 运维架构 > 网站架构

Varnish搭建缓存代理提高网站访问速度

2013-09-22 12:05 585 查看
简介:
Varnish是一款高性能、开源的反向代理服务器和缓存服务器,官方说是squid的四倍,实际应用测试中虽然达不到四倍的性能,那也能达到1-2倍的效果。
Varnish和Squid的对比:
Squid 也是一种开源的代理缓存软件,下面对比 Varnish 和 Squid 的不同点。
Varnish的稳定性很好。两者在完成相同负载的工作时,Squid服务器发生故障的几率要高于Varnish,因此Squid需要经常重启。Varnish访问速度更快。Varnish采用了 Visual Page Cache技术,所有缓存的数据都直接从内存读取,而Squid从硬盘读取缓存的数据,所以Varnish在访问速度方面会更快一些。Varnish可以支持更多的并发连接。因为Varnish的TCP连接与释放比Squid快,所以在高并发连接情况下可以支持更多的TCP连接。Varnish可以通过管理端口来管理缓存,使用正则表达式就可以批量清除部分缓存,而Squid做不到这一点。
Varnish缺点:
Varnish在高并发状态下,CPU、I/O和内存等资源的开销高于Squid。Varnish的进程一旦挂起、崩溃或者重启,缓存的数据都会从内存中释放出来。此时的所有请求都会被发送到后端应用服务器上,在高并发的情况下,就会给后端服务器造成很大压力。
一、安装varnish
1.下载:https://www.varnish-cache.org/releases/varnish-cache-3.0.3
yum install -y make automake pkgconfig libtool pcre-devel #安装依赖包
tar zxvf varnish-3.0.3.tar.gz
./autogen.sh
./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
make && make install
2.创建用户、组和日志文件

groupadd varnish
useradd -g varnish -s /sbin/nologin varnish
touch /usr/local/varnish/access.log #创建访问日志文件
chown -R varnish.varnish /usr/local/varnish
二、修改配置varnish

前端varnish缓存是以轮询的方式分担到后端web服务器,以下vcl不对php进行缓存。

vi /usr/local/varnish/etc/varnish/default.vcl
backend web1 {
.host = "192.168.0.203"; #后端web1服务器ip
.port = "80"; #通过80端口来访问后端web
.connect_timeout = 1s;  #连接超时时间
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;  #检测超时时间
.window = 5;
.threshold = 3;
}
}
backend web2 {
.host = "192.168.0.204";
.port = "80";
.connect_timeout = 1s;  #连接超时时间
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;  #检测超时时间
.window = 5;
.threshold = 3;
}
}
director default round-robin { #定义后端服务器组,默认轮询方式
{
.backend = web1;
}
{
.backend = web2;
}
}
sub vcl_recv {
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
}
else {
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.http.Authorization || req.http.Cookie) {
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 == "*") {
set beresp.ttl = 120 s;
return (hit_for_pass);
}
return (deliver);
}
sub vcl_deliver {
return (deliver);
}
sub vcl_error {
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);
}
语句解析:

Varnish处理请求的主要处理方法
1. vcl_recv
首先接收请求,判断是否要进一步处理,还是直接转发给后端(pass)等。 此过程中可以使用和请求相关的变量,例如客户端请求的url,ip,user-agent,cookie等,此过程中可以把不需缓存的地址,通过判断(相等、不相等、正则匹配等方法)转给后端,例如gif/png/jpg/css/js等静态文件;
2. vcl_fetch
当从后端服务器获取内容后会进入此阶段,除了可以使用客户端的请求变量,还可以使用从后端获取的信息(bersp),如后端返回的头信息,设置此信息的缓存时间TTL等;
3. vcl_miss
缓存未命中时中要做的处理
4. vcl_hit
缓存命中后做的处理
5. vcl_deliver
发送给客户端前的处理
6. vcl_pass
交给后端服务器
7. vcl_hash
设置缓存的键值key
Varnish处理流程
首次请求时过程如下:
recv->hash->miss->fetch->deliver
缓存后再次请求:
recv->hash->hit->deliver(fetch的过程没了,这就是我们要做的,把要缓存的页面保存下来)
直接交给后端pass的情况:
recv->hash->pass->fetch->deliver(直接从后端获取数据后发送给客户端,此时Varnish相当于一个中转站,只负责转发)三、测试1、启动
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80
-f:选项用于指定Varnishd使用的配置文件的路径。-s malloc,2G:–s选项用来确定Varnish使用的存储类型和存储容量,这里使用的是malloc类型(malloc是一个C函数,用于分配内存空间),2G 定义多少内存被malloced。-T:127.0.0.1:2000是Varnish基于文本方式的一个管理接口,启动后可以在不停止Varnish的情况下来管理Varnish。管理端口2000可以指定。因为不是任何人都可以访问Varnish管理端口,所以这里推荐只监听本机端口。-a:0.0.0.0:80中-a选项表示Varnish监听所有IP发给80端口的HTTP请求。
2、设置varnish访问日志
/usr/local/varnish/bin/varnishncsa -n /usr/local/varnish/var/varnish/test.com -a -w /usr/local/varnish/access.log & #将varnish访问日志写入到access.log里
/usr/local/varnish/var/varnish/test.com :默认缓存目录
netstat -tuplan |grep varnish #查看是否启动2000和80端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28551/varnishd
tcp 0 0 127.0.0.1:2000 0.0.0.0:* LISTEN 28550/varnishd
tcp 0 0 :::80 :::* LISTEN 28551/varnishd
curl -I http://ip #使用curl命令查看是否被缓存
killall -9 varnishd #关闭varnish
现在输入varnish服务器ip就可以访问到后端web服务器!3、设置开机启动:
echo '/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80' >> /etc/rc.local
4、添加多个后端服务器如果添加多个后端网站服务器,在default.vcl里面添加不同网站的访问请求转发到对应的backend去。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  varnish加速   var