14

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

目前,我需要为每个 URL 发出单独的命令,例如:

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 下的所有 URL,以及 example.com 子域中的所有 URL,基本上所有由 Varnish 管理的 URL。

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

这是我当前的 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

4 回答 4

16

使用 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"));
    }

    # ...
}
于 2016-09-30T14:56:14.420 回答
16

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

跑:sudo /etc/init.d/varnish restart

于 2017-07-21T13:46:34.863 回答
5

假设没有更改 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 在评论中指出的那样,这实际上会创建一个惰性失效(因此仅在请求时删除)。如果你想让这些对象经常被后台禁令潜伏者清除,你可以这样做:

# 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;
于 2016-08-11T14:09:23.560 回答
3

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

varnishadm "ban.url ."  # Matches all URLs

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

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

varnishadm "ban req.http.host == xxx.com"
于 2018-05-17T12:41:17.740 回答