varnish简介:varnish是一个开源的反向代理软件和HTTP加速器,是一个新贵的缓存软件,与缓存的元老squid相比,varnish更轻量级一些,varnish具有性能更高、速度更快、管理更方便。
软件环境:redhat6.5
实验环境:
server1:172.25.45.1
server2:172.25.45.2
iptables off and selinux disabled
varnish的安装:
所需的安装包:varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm
yum install varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm -y #安装
1
|
vim
/etc/sysconfig/varnish
|
将66行VARNISH_LISTEN_PORT改为80
66 VARNISH_LISTEN_PORT=80
1
|
vim
/etc/httpd/conf/httpd
.conf
|
将136行Listen值改为8080
Listen 8080
添加以下内容
1
|
vim
/etc/varnish/default
.vcl
|
backend default {
.host = "127.0.0.1";
.port = "8080";
}
1
2
|
/etc/init
.d
/httpd
start
/etc/init
.d
/varnish
start
|
1
|
netstat
-antlpe
|
添加测试页面:
1
|
echo
server1.example.com >
/var/www/html/index
.html
|
访问:172.25.45.1
显示:server1.example.com
1
|
curl -I 172.25.45.1
|
1
|
curl -I 172.25.45.1
/index
.html
|
curl -dump 172.25.45.1
测试:
可以更改/etc/sysconfig/varnish文件更新时间的设定
94 # # Default TTL used when the backend does not specify one
95 VARNISH_TTL=5
1
|
/etc/init
.d
/varnish
restart
|
1
|
echo
test
....>
/var/www/html/index
.html
|
5秒后刷新页面内容则更改
curl -I localhost #查看时间
curl -I localhost/index.html
curl -I -dump localhost/index.html #显示内容
##查看缓存命中情况
1
|
vim
/etc/varnish/default
.vcl
|
在backend default下添加以下内容:
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
1
|
/etc/init
.d
/varnish
reload
|
测试:
1
|
curl -I 172.25.45.1
/index
.html
|
注:会多出一行X-显cache示缓存命中情况
可以通过curl 172.25.45.1/index.html和curl 172.25.45.1查看缓存,然后修改iindex.html的内容,curl不更新,可以通过varnishadm ban.url .*$清除所有缓存
1
|
|
定义后端服务器:
1
|
vim
/etc/varnish/default
.vcl
|
添加以下内容:
制作一个测试页面:
1
2
|
echo
server1-www.westos.org >
/var/www/html/index
.html
echo
server2-bbs.westos.org >
/var/www/html/index
.html
|
1
|
|
在物理机中解析:vim /ect/hosts
测试:
浏览器访问:
把多个后端聚合为一个组,并检测后端健康状况:
1
|
vim
/etc/varnish/default
.vcl
|
在backend server2下添加以下内容:
director westoslb round-robin {
{.backend = default;}
{.backend = server2;}
}
将sub vcl_recv中第一个req.backend值改为westoslb
server2:
1
|
vim
/etc/httpd/conf/httpd
.conf
|
将990行 NameVirtualHost *:80 的注释取消
并在最后添加以下内容:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.westos.org
serveralias westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
1
2
3
4
|
mkdir
/bbs
echo
bbs.westos.org >
/bbs/index
.html
echo
server2-www.westos.org >
/var/www/html/index
.html
/etc/init
.d
/httpd
restart
|
server1:
/etc/init.d/varnish reload
http://bbs.westos.org/
显示bbs.westos.org
http://www.westos.org/
一开始显示server1-www.westos.org,过一会儿显示server2-www.westos.org
在set req.backend = westoslb;下添加以下内容:
return (pass);
1
|
/etc/init
.d
/varnish
reload
|
测试:
http://www.westos.org/
不断刷新,页面在server1-www.westos.org与server2-www.westos.org中切换
1
|
/etc/init
.d
/httpd
stop
|
页面将停在server2-www.westos.org,而不会切换
bansys:
所需要的软件包:bansys.zip
1
2
3
4
5
6
7
|
yum
install
-y unzip
unzip bansys.zip -d
/var/www/html/
yum
install
-y php
cd
/var/www/html/bansys/
mv
* ..
cd
..
rm
-rf bansys
|
1
|
vim config.php
|
将数据库信息10-22行注释掉
将 $var_group1的'host' => array的值改为当前虚拟机IP,端口改为6082;并将$var_group3注释掉
将$VAR_CLUSTER的内容改为'www.westos.org' => $var_group1,多余部分删掉
1
|
/etc/init
.d
/httpd
restart
|
测试:
http://172.25.45.1:8080/index.php
1
|
vim
/etc/varnish/default
.vcl
|
在sub vcl_recv下面添加以下内容:
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
在最上面添加以下内容:
acl westos {
"127.0.0.1";
"172.25.45.0"/24;
}
并将return (pass);注释掉
1
|
/etc/init
.d
/varnish
reload
|
测试:
http://www.westos.org/index.html
显示server1-www.westos.org,且刷新时不能切换
http://172.25.45.1:8080/index.php
选择HTTP,在推送内容中输入/index.html,并提交
打开http://www.westos.org/index.html,刷新后页面变为server2-www.westos.org
重复上述步骤,页面会切换成server1-www.westos.org
转载于:https://blog.51cto.com/hgh1882928/1841287