1.环境搭建:
需要两台开启http服务的虚拟机和一台开启varnish的虚拟机
在server1上get到varnish支持的软件包
测试:
在server1上:
vim /etc/security/limits.conf #系统给varhish文件的限制
51 varnish - nofile 131072
52 varnish - memlock 82000
53 varnish - nproc unlimited
vim /etc/varnish/default.vcl
7 backend default {
8 .host = “172.25.20.2”;
9 .port = “80”;
vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80
/etc/init.d/varnish restart
在server2上:
yum install httpd
cd /var/www/html
vim index.html
server2
/etc/init.d/httpd restart
在真机上:
curl 172.25.20.1
##varnish保留缓存
测试:在server1上:
vim /etc/varnish/default.vcl
12 sub vcl_deliver {
13 if (obj.hits>0){
14 set resp.http.X-Cache = “HIT from westos cache”;
15 }
16 else{
17 set resp.http.X-Cache = “MISS from westos cache”;
18 }
19 return (deliver);
20 }
/etc/init.d/varnish reload
##varnish一般都为热启动,所以为reload
在真机上:
curl 172.25.20.1 -I
[kiosk@foundation20 ~]$ curl 172.25.20.1 -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Tue, 02 Apr 2019 03:58:31 GMT
ETag: “200f9-8-5858426825049”
Content-Type: text/html; charset=UTF-8
Content-Length: 8
Accept-Ranges: bytes
Date: Tue, 02 Apr 2019 06:11:04 GMT
X-Varnish: 281154239
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
##第一次为MISS,因为没有缓存
[kiosk@foundation20 ~]$ curl 172.25.20.1 -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Tue, 02 Apr 2019 03:58:31 GMT
ETag: “200f9-8-5858426825049”
Content-Type: text/html; charset=UTF-8
Content-Length: 8
Accept-Ranges: bytes
Date: Tue, 02 Apr 2019 06:11:06 GMT
X-Varnish: 281154240 281154239
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
##第二次为“HIT”,因为已有第一次的缓存
##清除缓存:
在server1上:
varnishadm ban.url .*$
再次在真机上访问时就会出现“MISS”
在真机中访问
curl 172.25.20.1/index.html -I
varnishadm ban.url /index.html同理,测试时需要在后加上/index.html
url:统一地址标示符
##通过域名反向代理,把你带到不同的服务器
在server1上
vim /etc/varnish/default.vcl
12 backend web2 {
13 .host = “172.25.20.3”;
14 .port = “80”;
15 }
16
17 sub vcl_recv {
18 if(req.http.host ~"^(www.)?westos.org") {
19 set req.http.host = “www.westos.org”;
20 set req.backend = web1;
21 }elsif (req.http.host ~"^bbs.westos.org"){
22 set req.backend = web2;
23 }else {
24 error 404 “error”;
25 }
26
27
28 }
/etc/init.d/varnish reload ##重启服务
varnishadm ban.url .*$ ##清除已有的缓存
在server2和server3上分别配置好httpd服务
[root@server2 html]# vim index.html
server2:www.westos.org
/etc/init.d/httpd restart
[root@server3 ~]# vim /var/www/html/index.html
server3:bbs.westos.org
/etc/init.d/httpd restart
在真机上:
vim /etc/hosts #做好本地解析
172.25.20.1 server1 www.westos.org bbs.westos.org westos.org
[root@foundation20 ~]# curl www.westos.org
server2:www.westos.org
[root@foundation20 ~]# curl westos.org
server2:www.westos.org
[root@foundation20 ~]# curl bbs.westos.org
server3:bbs.westos.org
##实现负载均衡
在server1上:
vim /etc/varnish/default.vcl
17 director lb round-robin{
18 { .backend = web1;}
19 { .backend = web2;}
20 }
21
22 sub vcl_recv {
23 if(req.http.host ~"^(www.)?westos.org") {
24 set req.http.host = “www.westos.org”;
25 set req.backend = lb;
26 return (pass); ##清除缓存,让实验有效果
27 }elsif (req.http.host ~"^bbs.westos.org"){
28 set req.backend = web2;
29 }else {
30 error 404 “error”;
31 }
32
33
34 }
在server3上:
mkdir /www
vim /www/index.html
server3:www.westos.org
mkdir /bbs
vim /bbs/index.html
server3:www.westos.org
vim /etc/httpd/conf/httpd.conf ##编辑系统文件
990 NameVirtualHost *:80 ##开启端口
1011 <VirtualHost *:80>
1012 DocumentRoot /www
1013 ServerName www.westos.org
1014
1015
1016 <VirtualHost *:80>
1017 DocumentRoot /bbs
1018 ServerName bbs.westos.org
1019
/etc/init.d/httpd restart
在真机上:
[root@foundation20 ~]# curl www.westos.org
server2:www.westos.org
[root@foundation20 ~]# curl www.westos.org
server3:www.westos.org
[root@foundation20 ~]# curl bbs.westos.org
server3:bbs.westos.org
[root@foundation20 ~]# curl bbs.westos.org
server3:bbs.westos.org
实现轮循
缓存推送平台(图形界面推送)
##需要安装php支持
在server1上配置httpd服务
uxzip bansys.zip -d /var/www/html
将其解压到默认发布目录
cd /var/www/html
mv bansys/* .
vim config.php
1 <?php
2
3 //varnish主机列表
4 //可定义多个主机列表
5 $var_group1 = array(
6 ‘host’ => array(‘172.25.20.1’),
7 ‘port’ => ‘8080’,
8 );
9
10
11
12
13 //varnish群组定义
14 //对主机列表进行绑定
15 $VAR_CLUSTER = array(
16 ‘www.westos.com’ => $var_group1,
17 );
18
19
20 //varnish版本
21 //2.x和3.x推送命令不一样
22 $VAR_VERSION = “3”;
23
24 ?>
vim /etc/varnish/default.vcl
7 acl westos {
8 “127.0.0.1”;
9 “172.25.20.1”/24;
10 }
11
26 sub vcl_recv {
27 if (req.request == “BAN”) {
28 if (!client.ip ~ westos) {
29 error 405 “Not allowed.”;
30 }
31 ban("req.url ~ " + req.url);
32 error 200 “ban added”;
33 }
34 }
重启varnish服务
设置httpd服务
配置端口
在firefox中打开172.25.20.1:8080
用/index.html清除缓存
在真机中curl www.westos.org/index.html
再次清除缓存
在真机中
[root@foundation20 ~]# curl www.westos.org/index.html
server3:www.westos.org
[root@foundation20 ~]# curl www.westos.org/index.html
server2:www.westos.org
原理就是图形方式清除缓存。