Ich suche nach einer Möglichkeit, den Cache für alle Domains und alle URLs in Varnish zu leeren.
Derzeit müsste ich für jede URL einen eigenen Befehl eingeben, zum Beispiel:
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.
Während ich nach einer Möglichkeit suche, so etwas zu tun
curl -X PURGE http://example.com/*
Und das würde alle URLs unter example.com löschen, aber auch alle URLs in Subdomains von example.com, im Grunde alle URLs, die von Varnish verwaltet werden.
Irgendeine Idee, wie man das erreicht?
Dies ist meine aktuelle VCL-Datei:
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);
}
}
Mit Varnish 4.0 habe ich es schließlich mit dem Befehl ban
implementiert:
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"));
}
# ...
}
Nun, ich schlage vor, einfach den Lack neu zu starten. Alle Dateien werden gelöscht, da der Cache im Arbeitsspeicher gespeichert wird.
Ausführen: Sudo /etc/init.d/varnish restart
Wenn keine Änderung der URL oder des internen Cache-Schlüssels vorausgesetzt wird, wäre der einfachste Ansatz für eine vollständige Bereinigung ein Neustart von Varnish, da der Cache im Speicher erhalten bleibt.
Wenn ein schneller Neustart nicht akzeptabel ist, ist die von Rastislav vorgeschlagene BAN ein guter Ansatz. Es muss solange aktiv bleiben, wie lange Ihre längste TTL ist. Wenn Sie also häufig eine vollständige Spülung benötigen, ist die BAN-Liste ziemlich permanent, da der Verbot-Lurker (der für BANs, die nicht mehr relevant sind) immer der Meinung ist, dass Ihr BAN gilt ist nützlich
In Ihrem Fall wäre Ihre VCL also:
# 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"));
}
}
Wie von Carlos in den Kommentaren erwähnt, wird dies jedoch zu einer Ungültigerklärung von lazy führen (und daher erst zum Zeitpunkt der Anfrage entfernt). Wenn Sie möchten, dass die Objekte von background ban lurker regelmäßig gelöscht werden, können Sie stattdessen Folgendes tun:
# 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;
}
Dann machen Sie die Spülung:
curl -X BAN http://example.com;
Den gesamten Lack-Cache von der Befehlszeile löschen (den gesamten Cache ungültig machen):
varnishadm "ban.url ." # Matches all URLs
Hinweis: Der Befehl ist purge.url in Varnish 2.x.
Wir können auch durch einen Hostnamen verbieten:
varnishadm "ban req.http.Host == xxx.com"