如何清除Varnish中的完整缓存?

时间:2016-08-11 09:10:29

标签: caching varnish varnish-vcl varnish-4 clear-cache

我正在寻找一种清除Varnish中所有域和所有网址的缓存的方法。

目前,我需要为每个网址发布单独的命令,例如:

curl -X PURGE http://example.com/url1
curl -X PURGE http://example.com/url1
curl -X PURGE http://subdomain.example.com/
curl -X PURGE http://subdomain.example.com/url1
// etc.

虽然我正在寻找一种方法来做类似

的事情
curl -X PURGE http://example.com/*

这样可以清除example.com下的所有网址,还可以清除example.com子网中的所有网址,基本上是Varnish管理的所有网址。

知道如何实现这个目标吗?

这是我目前的VCL文件:

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Command to clear the cache
    # curl -X PURGE http://example.com
    if (req.method == "PURGE") {
        return (purge);
    }
}

4 个答案:

答案 0 :(得分:9)

使用Varnish 4.0,我最终使用ban命令实现它:

sub vcl_recv {
    # ...

    # Command to clear complete cache for all URLs and all sub-domains
    # curl -X XCGFULLBAN http://example.com
    if (req.method == "XCGFULLBAN") {
        ban("req.http.host ~ .*");
        return (synth(200, "Full cache cleared"));
    }

    # ...
}

答案 1 :(得分:4)

好吧,我建议重新启动清漆。它将清除所有文件,因为varnish将缓存保留在内存中。

运行:sudo /etc/init.d/varnish restart

答案 2 :(得分:3)

假设没有更改URL或内部缓存键,对于完全刷新,最简单的方法是重新启动Varnish,因为它在内存中维护其缓存。

如果不能接受快速重启,Rastislav建议的BAN是一个很好的方法。它需要保持活动,只要你最长的TTL,所以如果你经常需要一个完全刷新,BAN列表将是非常永久的,因为禁用潜伏者(扫描不再相关的BAN)可能总是认为你的BAN很有用

所以在你的情况下,你的VCL将是:

# Highly recommend that you set up an ACL for IPs that are allowed
# to make the BAN call
acl acl_ban {
    "localhost";
    "1.2.3.4"/32;
}

sub vcl_recv {
   if (client.ip ~ acl_ban && req.method == "BAN") {
      ban("req.http.host == " + req.http.host);
      # Throw a synthetic page so the request won't go to the backend.
      return(synth(200, "Ban added"));
   }
}

然而正如Carlos在评论中所指出的,这实际上会创建一个 lazy 失效(因此只在请求时删除)。如果您想让background ban lurker实际上每隔一段时间对对象进行一次清除,您可以这样做:

# Highly recommend that you set up an ACL for IPs that are allowed
# to make the BAN call
acl acl_ban {
    "localhost";
    "1.2.3.4"/32;
}

sub vcl_recv {
   if (client.ip ~ acl_ban && req.method == "BAN") {
      # see below for why this is obj. rather than req.
      ban("obj.http.host == " + req.http.host);
      # Throw a synthetic page so the request won't go to the backend.
      return(synth(200, "Ban added"));
   }
}

sub vcl_backend_response {
   # add any portions of the request that would want to be able
   # to BAN on. Doing it in vcl_backend_response means that it
   # will make it into the storage object
   set beresp.http.host = bereq.http.host;
}

sub vcl_deliver {
   # Unless you want the header to actually show in the response,
   # clear them here. So they will be part of the stored object
   # but otherwise invisible
   unset beresp.http.host;
}

然后进行冲洗:

curl -X BAN http://example.com;

答案 3 :(得分:1)

从命令行清除所有Varnish缓存(使所有缓存无效):

varnishadm "ban.url ."  # Matches all URLs

注意:命令是Varnish 2.x中的purge.url。

我们也可以通过主机名禁止:

varnishadm "ban req.http.host == xxx.com"
相关问题