如何清除 Varnish 中的完整缓存?
How to clear complete cache in Varnish?
我正在寻找一种方法来清除 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,因为它在内存中维护其缓存。
如果不能接受快速重启,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;
对于 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"));
}
# ...
}
好吧,我建议重新启动清漆。它会清除所有文件,因为清漆将缓存保存在内存中。
运行: sudo /etc/init.d/varnish restart
从命令行清除所有 Varnish 缓存(使所有缓存无效):
varnishadm "ban.url ." # Matches all URLs
注意:Varnish 中的命令是 purge.url 2.x。
我们也可以通过主机名禁止:
varnishadm "ban req.http.host == xxx.com"
我正在寻找一种方法来清除 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,因为它在内存中维护其缓存。
如果不能接受快速重启,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;
对于 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"));
}
# ...
}
好吧,我建议重新启动清漆。它会清除所有文件,因为清漆将缓存保存在内存中。
运行: sudo /etc/init.d/varnish restart
从命令行清除所有 Varnish 缓存(使所有缓存无效):
varnishadm "ban.url ." # Matches all URLs
注意:Varnish 中的命令是 purge.url 2.x。
我们也可以通过主机名禁止:
varnishadm "ban req.http.host == xxx.com"