本文会在新的centos7系统下安装varnish 软件,所遇到的问题都会在此处给记录下来,希望能给学习varnish的各位提供一点帮助。
1.yum install 报错 :大致就是刚安装完centos,没有配置dns
解决方案:
1. cd etc/sysconfig/network-scripts/
2. vi ifcfg-ens33
3.修改ONBOOT 和 NM_CONTROLLED 如下图
4.重启系统
2. (1) yum install epel-release 安装epel源
(2) yum install varnish
等待安装完成之后,就会在etc/varnish目录下生成varnish 的配置文件
修改default.vcl 和 varnish.params(修改监听端口)
default.vcl:
#必须以vcl git 开头
vcl 4.0;
import std;
#导入directors模块;并定义acl
import directors;
acl purgers {
"127.0.0.0"/8;
}
# backend组即为后台web端
backend server1 {
.host = "192.168.184.129";
.port = "80";
}
#以轮询方式调度
sub vcl_init {
new srvs =directors.round_robin();
srvs.add_backend(server1);
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
set req.backend_hint=srvs.backend();
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
#定义缓存响应头部
if (obj.hits>0) {
set resp.http.X-Cache = "HIT via " + server.ip;
} else {
set resp.http.X-Cache = "MISS from " + server.ip;
}
}
#执行lookup后,Varnish在缓存中没有找到请求的内容时会自动调用该方法。此模块可以用于判断>是否需要从后端服务器获取内容。
#vcl_fetch模块
sub vcl_miss {
if (req.method == "PURGE") {
return (synth(404, "Purged."));
}
return (fetch);
}
其中varnish 的ip是192.168.184.128,后台web端的ip是192.168.184.129,web端就新建了一个html页面
启动varnish:service varnish start
关闭防火墙 : systemctl stop firewalld.service
启动httpd服务:systemctl start httpd.service
然后打开浏览器,访问如图:
1.第一次是miss 但是依然调用后台获取到页面
2.第二次就是hit 了
另外,想学习vcl的语法知识可以去:https://blog.csdn.net/wos1002/article/details/56483289
如果文中有什么错误,欢迎指出,以免更多的人被误导。谢谢。