实验环境(Centos7)
主机名 IP 版本
varnish 192.168.14.210 4.0.5
web1(nginx) 192.168.14.211 nginx/1.12.2
client 192.168.14.212
一、varnish
官网地址:http://varnish-cache.org/trac/wiki/VCLExamples
1、关闭防火墙和selinux
[root@varnish ~]# systemctl stop firewalld
[root@varnish ~]# systemctl disable firewalld
[root@varnish ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2、配置阿里epel源
[root@varnish ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
3、安装varnish
[root@varnish ~]# yum install -y varnish
修改前备份配置文件(可选)
[root@varnish ~]# cp /etc/varnish/varnish.params{,.bak}
[root@varnish ~]# cp /etc/varnish/default.vcl{,.bak}
4、更改默认端口
更改前:
[root@varnish ~]# cat /etc/varnish/varnish.params|grep VARNISH_LISTEN_PORT
VARNISH_LISTEN_PORT=6081
[root@varnish ~]# vim /etc/varnish/varnish.params
更改后:
[root@varnish ~]# cat /etc/varnish/varnish.params|grep VARNISH_LISTEN_PORT
VARNISH_LISTEN_PORT=80
5、修改配置文件(简单配置只需修改两个地方)
backend default {
.host = "192.168.14.211";
.port = "80";
}
sub vcl_deliver {
if (obj.hits>0) {
set resp.http.X-Cache = "Hit via "+ server.ip;
}else {
set resp.http.X-Cache = "Miss via "+ server.ip;
}
}
6、启动varnish服务并开机启动
[root@varnish ~]# systemctl start varnish
[root@varnish ~]# systemctl enable varnish
二、web
1、同上,关闭防火墙和selinux
2、同上,配置阿里epel源
3、安装nginx
[root@web1 ~]# yum install -y nginx
4、修改自定义主页
[root@web1 ~]# echo "nginx webserver1" > /usr/share/nginx/html/index.html
[root@web1 ~]# cat /usr/share/nginx/html/index.html
nginx webserver1
5、启动服务
[root@web1 ~]# systemctl start nginx
[root@web1 ~]# systemctl enable nginx
三、客户端访问
1、centos访问
2、window访问
刷新浏览器,返回304状态码,并且是缓存服务器返回的数据
3、至此,varnish缓存服务器搭建成功