服务器IP配置如图:
OS |
IP |
子网掩码 |
路由网关 |
Centos6.6 (Web1静态) |
Eth0:192.168.10.1 |
255.255.255.0 |
|
Centos6.6 (Web2动态) |
Eth0:192.168.10.2 |
255.255.255.0 |
|
Centos6.6 (varnish) |
Eth0:192.168.26.210 |
255.255.252.0 |
192.168.25.3 |
Eth1:192.168.10.3 |
255.255.255.0 |
安装varnish:yum install varnish –y
Rpm –ql varnish #查看varnish配置信息
启动varnish:
service varnish start
ss -tnlp #6082端口为管理端口,80为代理端口
登陆varnish管理:
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
手动创建一个简单VCL:
vim /etc/varnish/test.vcl
backend webser1 {
.host = "192.168.10.1";
.port = "80";
}
vcl.load test1 /etc/varnish/test.vcl #指定VCL配置文件需要绝对路径否则报错
vcl.list #显示已经存存的配置项和状态信息
使用某个设置项:
vcl.use test1 #test1为配置项名字
查看访问状态:
vcl.use test3
再查看访问状态:
配置文件(动静分离web站后端服务器缓存):
[[email protected] varnish]# vim test1.vcl
backend web1 {
.host = "192.168.10.1";
.port = "80";
}
backend web2 {
.host = "192.168.10.2";
.port = "80";
}
sub vcl_deliver {
if (obj.hits > 0 ) {
set resp.http.X-Cache = "HIT form" server.ip;
} else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_recv {
if (req.url ~ "\.(html|css|js)$") {
set req.backend = web1;
} else {
set req.backend = web2;
}
}
访问:http://192.168.26.210/index.html
http://192.168.26.210/test.html
访问的内容为web1上内容
输入:http://192.168.26.210/index.php
http://192.168.26.210/baidu.jpg
展示为:web2网站内容
顺便测试下缓存的图片效果:将web上的图片文件更改名字再进行访问
过两分钟后再访问缓存失效了,访问会报错