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

lau*_*ent 9 caching varnish clear-cache varnish-vcl varnish-4

我正在寻找一种方法来清除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.
Run Code Online (Sandbox Code Playgroud)

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

curl -X PURGE http://example.com/*
Run Code Online (Sandbox Code Playgroud)

这将清除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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tor 11

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

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

  • 这将导致服务器停机。我建议运行varnishadm“ ban req.url〜/”代替 (2认同)

lau*_*ent 10

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

    # ...
}
Run Code Online (Sandbox Code Playgroud)


Jos*_*ald 6

假设 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"));
   }
}
Run Code Online (Sandbox Code Playgroud)

然而,正如卡洛斯在评论中指出的,这实际上会创建一个惰性失效(因此仅在请求时删除)。如果您想让对象经常被背景禁令潜伏者清除,您可以这样做:

# 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;
}
Run Code Online (Sandbox Code Playgroud)

然后进行冲洗:

curl -X BAN http://example.com;
Run Code Online (Sandbox Code Playgroud)

  • 没错,但请注意,您在“vcl_recv”中创建的禁令对潜伏者并不友好。它将被禁令潜伏者忽略。在某些情况下,这并不重要,但如果您的禁令要与存储中的许多对象匹配,您应该避免惰性失效并允许禁令潜伏者清除对象。创建对潜伏者友好的禁令需要更多的 VCL 行。请查看 https://www.varnish-cache.org/docs/trunk/users-guide/purging.html#bans 了解详细信息。 (2认同)

Ash*_*win 5

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

varnishadm "ban.url ."  # Matches all URLs
Run Code Online (Sandbox Code Playgroud)

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

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

varnishadm "ban req.http.host == xxx.com"
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不可能以任何简单的方式进行..#upvoteregrets (2认同)