Caching 如何清除清漆中的完整缓存?

Caching 如何清除清漆中的完整缓存?,caching,varnish,varnish-vcl,varnish-4,clear-cache,Caching,Varnish,Varnish Vcl,Varnish 4,Clear Cache,我正在寻找一种方法来清除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. 而我正在寻找一种方法来做类似的事

我正在寻找一种方法来清除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);
    }
}

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

如果快速重启是不可接受的,拉斯蒂斯拉夫建议的禁令是一个很好的方法。它需要在你最长的TTL期间保持活跃,因此如果你经常需要完全刷新,禁令列表将非常永久,因为禁令潜伏者(扫荡不再相关的禁令)可能总是认为你的禁令有用

因此,在您的情况下,您的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;

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

    # ...
}

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


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

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

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

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

varnishadm "ban req.http.host == xxx.com"

你试过使用banning吗?这可能正是你想要的。没错,但请注意,您在
vcl\u recv
中创建的禁令对潜伏者并不友好。这将被禁令潜伏者忽视。在某些情况下,这并不重要,但如果您的禁令将匹配存储中的许多对象,则应避免延迟失效,并允许禁令潜伏者清除对象。创建对潜伏者友好的禁令需要多行VCL。请查看详细信息。这是一个很好的注释!我会考虑到这一点修改答案。不需要写VCL来提交禁令。在任何情况下,我们都应该建立一个禁止潜伏者的友好关系。有关详细信息,请参阅。虽然这种方法可行,但对于包含大量对象的缓存来说,效率不是很高。我建议未来的读者看看Joshua DeWald下面的答案,这是一个对潜伏者友好的答案,因此在清漆上更容易处理。之后我该如何解开它?这似乎不可能以任何简单的方式实现#upvoteregrets似乎在varnish5中,他们稍微更改了命令。已使用
varnishadm'ban req.url~清除所有varnish缓存。
这将在服务器上造成停机。我建议改为运行varnishadm“ban req.url~/”
varnishadm "ban req.http.host == xxx.com"