(基于Red Hat rhel6.5)
一.Varnish简介
Varnish是一款高性能且开源的反向代理服务器和http加速器 挪威的最大的在线报纸 Verdens Gang(vg.no) 使用 3 台 Varnish 代替了原来的 12 台 Squid,性能比以前更好,这是 Varnish 最成功的应用案例。 Varnish流程图:
二.安装Varnish
varnish的安装不是直接在yum仓库里安装,而是将rpm包从官网下载下来,
在安装包的目录下,执行yum install * -y 就可以进行安装了。
Varnish的rpm包:
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
三.反向代理
vim /etc/sysconfig/varnish #进入此文件里,进行实验环境的配置 66 VARNISH_LISTEN_PORT=80 改为80端口,varnish直接去监听80端口 vim /etc/varnish/default.vcl 7 backend default { 8 .host = “172.25.50.2”; #反向代理去取数据的地方 9 .port = “80”; 10 } 添加缓存命中代码和轮循代码: 定义多个不同域名站点的后端服务器
backend web1 {
.host = "172.25.50.2";
.port = "80";
}
backend web2 {
.host = "172.25.50.3";
.port = "80";
}
director lb round-robin {
#将多个后端聚合成一个名为lb的组,轮叫模式
{ .backend = web1; }
{ .backend = web2; }
}
#访问www.wrh.org域名,到web1,访问bbs.wrh.org到web2
sub vcl_recv { if (req.http.host ~ "^(www.)?wrh.org") { set req.http.host = "www.wrh.org"; set req.backend = lb; return (pass);
#pass将当前请求直接转发到后端服务器。pass通常只处理静态页面。varnish也只缓存静态数据
} elsif (req.http.host ~ "^bbs.wrh.org") {
set req.backend = web2;
} else {
error 404 "wrh cache";
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from wrh cache";
}
else {
set resp.http.X-Cache = "MISS from wrh cache";
}
return (deliver);
}
/etc/init.d/varnish reload#重载varnish服务配置
提示:到172.25.50.2上开启httpd服务,并且在/var/www/html目录下,创建index.html
(如果不改变配置文件的名称,必须要用index.html)
四.查看缓存命中情况
(缓存120秒清除一次,在这120秒内,显示是击中的) curl -I bbs.wrh.org
五.手动清除缓存
varnishadm ban.url .*$ #清除所有 varnishadm ban.url /index.html #清除 index.html 页面缓存 varnishadm ban.url /admin/$ #清除 admin 目录缓存
六.apach主机配置
在server2中(httpd): 1.yum install -y httpd cd /var/www/html/ vim index.html -->>www.wrh.org /etc/init.d/httpd restart 在server3中(httpd): 1.yum install -y httpd cd /var/www/html/ vim index.html -->>bbs.wrh.org /etc/init.d/httpd restart 提示:在物理机上测试的时候,记得要在物理机上添加解析,vim /etc/hosts
七.测试结果
轮循模式:
END