可以从官网获取varnish的安装包
(1)(varnish端)server1:
yum install * -y ##安装varnish
vim /etc/sysconfig/varnish ##主配置文件
VARNISH_LISTEN_PORT=80 ##监听80端口
/etc/init.d/varnish start ##开启varnish
vim /etc/varnish/default.vcl ##默认文件
backend default {
.host = "172.25.32.2"; ##主服务端
.port = "80"; ##端口
}
##查看缓存命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache"; ##hit命中
}
else {
set resp.http.X-Cache = "MISS from westos cache"; ##miss命中第一下
}
return (deliver);
}
/etc/init.d/varnish reload ##重新加载varnish
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2017-07-20T06:53:13
VCL compiled.
available 0 boot
active 0 reload_2017-07-20T06:53:13
Done ##这里表示重新加载成功
(2)(server服务器端)server2:
yum install httpd -y
service httpd start ##开启httpd服务
vim /var/www/html/index.html ##编辑apache默认发布页
(3)测试端
/etc/hosts
172.25.32.1 server1 www.westos.com
测试:
浏览器访问 172.25.32.1 和 www.westos.org
发现页面是server2的页面
curl www.westos.org -I
MISS未命中
HIT
varnishadm ban.url .*$ ##清除所有缓存
curl www.westos.org -I
varnishadm ban.url / ##清除站的缓存
curl www.westos.org -I
varnishadm ban.url /index.html ##清除站的默认访问页面的缓存
curl www.westos.org -I
curl www.westos.org/index.html -I
测试:
curl www.westos.org -I
MISS | HIT
(1)server1(varnish端):
vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.32.2";
.port = "80";
}
backend web2 {
.host = "172.25.32.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org"; ##以www.开头或不开头的westos.org默认到www.westos.org
set req.backend = web1; ##在web1上取数据
} elsif (req.http.host ~ "^bbs.westos.org") { ##bbs.westos.org则在web2取数据
set req.backend = web2;
} else {error 404 "westos cache"; ##其他(即ip)则显示404错误
}
}
/etc/init.d/varnish reload ##重新加载varnish服务
(2)测试端
vim /etc/hosts
172.25.32.1 server1.example.com www.westos.org westos.org bbs.westos.org
测试:
[root@foundation32 ~]# curl www.westos.org
server2
[root@foundation32 ~]# curl westos.org
server2
[root@foundation32 ~]# curl bbs.westos.org
server3
[root@foundation32 ~]# curl 172.25.32.1
(1)server1(varnish):
vim /etc/varnish/default.vcl
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb; ##www.westos指定lb
return (pass); ##直接varnish到服务端轮询
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web1; ##bbs指定web1
} else {error 404 "westos cache";
}
}
(2)server2(server服务端):
建立apache虚拟主机
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
DocumentRoot /var/www/html
ServerName server2
##bbs虚拟主机
DocumentRoot /www/bbs
ServerName bbs.westos.org
##www.westos虚拟主机
DocumentRoot /www/westos
ServerName www.westos.org
mkdir -p /www/bbs
mkdir /www/westos
vim /www/bbs/index.html
vim /www/westos/index.html
/etc/init.d/httpd restart ##重启httpd服务
(3)server3(server服务端):
vim /var/www/html/index.html
(4)测试
varnishadm ban.url /
curl www.westos.org
curl bbs.westos.org
不加pass时:需要清除缓存才能跳到另一个页面(页面缓存时间是120s)
加了pass时:
(1)varnish端(server1):
yum install php -y ##下载php
yum install httpd -y ##下载apache
vim /etc/httpd/conf/httpd.conf ##编辑apache配置文件
Listen 8080 ##使http监听8080端口(varnish已经监听了80端口)
unzip bansys.zip -d /var/www/html/
mv /var/www/html/bansys/* /var/www/html
rm -fr /var/www/html/bansys/
vim /var/www/html/config.php ##编辑推送平台配置
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.32.1',),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
/etc/init.d/httpd start ##开启httpd服务
vim /etc/varnish/default.vcl ##编辑配置文件
需要把pass注释掉
acl westos { ##westos的访问控制
"127.0.0.1";
"172.25.32.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";
}
}
/etc/init.d/varnish reload ##重新加载varnish服务
(2)测试:
浏览器访问 www.westos.org/index.html
浏览器访问 172.25.32.1:8080