1. 安装(192.168.0.50)
http://repo.varnish-cache.com/RedHat/varnish-3.0/el6/x86_64/
yum localinstall -y varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
2. 配置
# vi /etc/varnish/default.vcl
###配置一个后端服务器
backend web1 {
.host = "192.168.0.1";
.port = "80";
}
###查看缓存命中情况
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from linuxidc cache";
}
else {
set resp.http.X-Cache = "MISS from linuxidc cache";
}
return (deliver);
}
###配置varnish服务端口
# vi /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80
# service varnish start
3.测试缓存命中
# curl -I 192.168.0.50
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT
ETag: "1c13aa-16-4c7b4135e08a6"Content-Type: text/html; charset=UTF-8
Content-Length: 22
Accept-Ranges: bytes
Date: Fri, 24 Aug 2012 14:30:40 GMT
X-Varnish: 766467032
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from linuxidc cache #未命中
# curl -I 192.168.0.50
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT
ETag: "1c13aa-16-4c7b4135e08a6"
Content-Type: text/html; charset=UTF-8
Content-Length: 22
Accept-Ranges: bytes
Date: Fri, 24 Aug 2012 14:30:54 GMT
X-Varnish: 766467033 766467032
Age: 14
Via: 1.1 varnish
Connection: keep-alive
X-Cache:HIT from linuxidc cache #命中
### 通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除index.html页面缓存
# varnishadm ban.url /admin/$ #清除admin目录缓存
4.定义多个不同域名站点的后端服务器
#vim /etc/varnish/default.vcl
backend web1 {
.host = "192.168.0.1";
.port = "80";
}
backend web2 {
.host = "192.168.0.2";
.port = "80";
}
#当访问www.linuxidc.com域名时从web1上取数据,访问bbs.linuxidc.com域名时到web2取数据,访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?linuxidc.com") {
set req.http.host = "www.linuxidc.com";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.linuxidc.com") {
set req.backend = web2;
} else { error 404 "linuxidc cache";
}
}
sub vcl_deliver {
if (obj.hits > 0){
set resp.http.X-Cache = "HIT from linuxidc cache";
}
else {
set resp.http.X-Cache = "MISS from linuxidc cache";
}
return (deliver);
}
# service varnish reload
网页访问web:www.linuxidc.com web:bbs.linuxidc.com则会显示不同的内容。
5.定义负载均衡
##添加虚拟主机
vim /etc/httpd/conf/httpd.conf(192.168.0.2上的操作)
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.linuxidc.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/web2 (你要自己创建这么目录哦!)
ServerName bbs.linuxidc.com
</VirtualHost>
#echo bbs.linuxidc.com > /var/www/web2/index.html
#service httpd restart
##定义健康检查
probe healthcheck {
.url = "/index.html"; # 哪个 url需要varnish请求
.interval = 5s; #检查的间隔时间
.timeout = 1s; #等待多长时间探针超时
.window = 5; #维持5个sliding window的结果
.threshold = 3; #至少有三次window是成功的,就宣告bachend健康
}
backend web1 {
.host = "192.168.0.1";
.port = "80";
.probe = healthcheck;
}
backend web2 {
.host = "192.168.0.2";
.port = "80";
.probe = healthcheck;
}
director lb round-robin { #把多个后端聚合为一个组,并检测后端健康状况
{ .backend = web1; }
{ .backend = web2; }
}sub vcl_recv {
if (req.http.host ~ "^(www.)?linuxidc.com") {
set req.http.host = "www.linuxidc.com";
set req.backend = lb;
return (pass); #为了测试方便,不进行缓存。
} elsif (req.http.host ~ "^bbs.linuxidc.com") {
set req.backend = web2;
} else {
error 404 "linuxidc cache";
}
}
# service varnish reload
这时web访问http://www.linuxidc.com/就会显示不同的内容,即实现了负载均衡。
6.varnish cdn推送平台
http://code.google.com/p/varnish-php-bansys/
#需要安装php支持
# unzip bansys.zip -d /var/www/html
# vi /var/www/html/bansys/config.php #只保留如下设置,其余注释掉(注意:注释掉并不是删除掉,删除会造成推送网页的乱码!)
<?php
$var_group1 = array(
'host' => array('192.168.0.50'),
'port' => '6082',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.linuxidc.com' => $var_group1,
);
//varnish版本 //2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
#bansys有两种工作模式,分别是:telnet和http模式。
#telnet模式需要关闭varnish服务管理端口的验证,注释掉/etc/sysconfig/varnish “ 文件中的 -S ${VARNISH_SECRET_FILE}”这行,重启varnish服务即可。
#如果是http模式需要对varnish做以下设置:
# vi /etc/varnish/default.vcl
acl linuxidc {
#设置访问控制
"127.0.0.1";
"192.168.0.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ linuxidc) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
修改httpd的端口号
#vim /etc/httpd/conf/httpd.conf Listen 8080(大概是136行)
# service varnish reload


##推送是为了更新cache,让原网页失效,这样从外网访问内网的时候就是最新的数据。
Varnish 的详细介绍:请点这里
Varnish 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105124.htm
