varnish配置通过url清除缓存

本文介绍了如何配置Varnish的vcl.conf文件,以便通过构造特定URL(如http://xxx/~purge/yyyy)来清除相应页面(如http://xxx/yyyy)的缓存。在VCL的`vcl_fetch`和`vcl_hit`子例程中添加了判断和处理逻辑,使用`restart`关键字,并强调在Varnish 3.x之后的版本中,`purge`操作只能在`vcl_hit`中执行。
摘要由CSDN通过智能技术生成

vcl普通配置搜索下就很多

这里介绍下 配置varnish vcl.conf 通过构造特殊Url清理缓存

比如:

 在访问 http://xxx/~purge/yyyy时打开  http://xxx/yyyy  并清除掉该页缓存

sub vcl_fetch中加入
      if ( req.url ~ "^/~purge/" ) {
              # 判断是~purge开头的替换到~purge这个标识符
                set req.url = regsub (req.url , "^/~purge/" ,"/");
                return(restart);
        }
sub vcl_hit 中加入
if ( req.restarts == 1){
#如果是重写了url后匹配过来的

 if (!client.ip ~ purge) {   #只运行特殊IP这么干哦

 error 405 "Not allowed." ;
           }

                purge;
        }

restart 这个是重点 在3.x以后的版本 purge只能在 vcl_hit中执行 不能指定地址了

附:
资料  https://www.varnish-cache.org/trac/wiki/VCLExampleRestarts
Varnish配置文件VCL中的函数详解
  Called after a request is received from the browser, but before it is processed.
     接收到浏览器请求后,在处理请求之前调用此函数
vcl_pipe()
  Called when a request must be forwarded directly to the backend with minimal handling by Varnish (think HTTP CONNECT)
     当请求以varnish最小的处理而必须直接转交给后端服务时调用此函数

vcl_hash()
  Called to determine the hash key used to look up a request in the cache.
     当确定为请求的数据进行缓存指定哈希key时,调用此函数

vcl_hit()
  Called after a cache lookup when the object requested has been found in the cache.
     当在缓存中找到所要请求的数据时调用此函数

vcl_miss()
  Called after a cache lookup when the object requested was not found in the cache.
      当无法在缓存中找到所要请求的数据时调用此函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值