centos7下varnish4.1.10的安装和配置

本文会在新的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

如果文中有什么错误,欢迎指出,以免更多的人被误导。谢谢。

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值