Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79529
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-17 16:28
个人简介

喜欢折腾。

文章分类

全部博文(13)

文章存档

2016年(7)

2015年(6)

我的朋友

分类: 系统运维

2016-03-07 21:49:57

主机环境 rhel6 selinux  and iptables disabled
实验主机  172.25.254.2    varnish
                 172.25.254.3    apache
                  172.25.254.4   apache

首先得到这两个包 
varnish-3.0.5-1.el6.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm
安装
yum install  varnish-* -y

###配置一个后端服务器

Vim   /etc/varnish/default.vcl
backend xp1 {
.host = "172.25.254.3";
.port = "80";
}
这里写的3为后端,那先给它创建一个首页,方便测试
###配置 varnish 服务端口
vi /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80
/etc/init.d/varnish  restart

为了测试显示更加清晰在vcl中添加如下代码
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from  xp1  cache";
}
else {
set resp.http.X-Cache = "MISS from  xp1 cache";
}
return (deliver);
}
/etc/init.d/varnish  restart

###测试缓存命中
在4上  
[root@vm4 ~]# curl   172.25.254.2
haha-vm3

age 后面是时间,默认是120妙
HIT就代表命中    MISS就代表去后端查询去了

###通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$
#清除所有
# varnishadm ban.url /index.html
#清除 index.html 页面缓存
# varnishadm ban.url /admin/$
#清除 admin 目录缓存

###定义多个不同域名站点的后端服务器
backend xp1 {
.host = "172.25.39.3";
.port = "80";
}
backend xp2 {
.host = "172.25.39.4";
.port = "80";
}

#当访问 域名时从 xp1 上取数据,访问 bbs.westos.org 域名时到 xp2 取数据,访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.com") {
set req.http.host = "";
set req.backend = xp1;
} elsif (req.http.host ~ "^bbs.westos.com") {
set req.backend = xp2;
} else {error 404 "westos cache";
}
}
在那个里面测试就在里面加  vim /etc/hosts
                                                    172.25.254.2         bbs.westos.com


###定义负载均衡
director lb round-robin {
{ .backend = xp1;}
{.backend = xp2;}
}
上面采用rr算法,还有其他算法如hash,fallback,random等

sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.com") {
set req.http.host = "";
set req.backend = lb;
return (pass);       #为了测试方便,不进行缓存。
} elsif (req.http.host ~ "^bbs.westos.com") {
set req.backend = xp2;
} else {
error 404 "westos cache";
}
}
测试效果

###varnish cdn 推送平台

#需要安装 php 支持
unzip bansys.zip   -d   /var/www/html
 vim   /var/www/html/bansys/config.php         #只保留如下设置,其余注释掉
cd  /var/www/html/bansys/
cp  *.php     ..
$var_group1 = array(
'host' => array('172.25.254.2'),
'port' => '6082',
);
//varnish 群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'' => $var_group1,
);
//varnish 版本//2.x 和 3.x 推送命令不一样
$VAR_VERSION = "3";
?>
bansys 有两种工作模式,分别是:telnet 和 http 模式。
telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的“-S $
{VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
如果是http模式需要做如下设置:
vim    /etc/varnish/default.vcl
acl westos {
#设置访问控制
"127.0.0.1";
"172.25.254.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
然后在浏览器中就可以查看了


























阅读(2296) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~