我有 2 个 django 项目,设置基本相同。它们都是新闻/文章页面,问题出在第二个页面。第一个项目缓存正确(索引页和文章页),虽然第二个项目只缓存主页,例如:
项目1主页:
curl -X PURGE http://teste.infalms.com.br/ (response: 200[hit] purged)
项目 1 文章页面:
curl -X PURGE http://teste.infalms.com.br/noticias/variedades/pqp-hein/ (response: 200[hit] purged)
项目 2 主页:
curl -X PURGE http://pcmag.infalms.com.br/ (response: 200[hit]
项目 2 文章页面(404 [错过] 不在缓存中。):
curl -X PURGE http://pcmag.infalms.com.br/noticias/google-street-view-continua-capturando-momentos-estranhos/ 404 [miss] Not in cache.
这是我的 default.vcl:
backend teste {
.host = "127.0.0.1";
.port = "8080";
}
acl purge {
"localhost";
"127.0.0.1";
"198.58.107.57";
}
sub vcl_recv {
# unless sessionid/csrftoken is in the request, don't pass ANY cookies (referral_source, utm, etc)
if (req.request == "GET" && (req.url ~ "^/static" || (req.http.cookie !~ "sessionid" && req.http.cookie !~ "csrftoken"))) {
remove req.http.Cookie;
}
# normalize accept-encoding to account for different browsers
# see: https://www.varnish-cache.org/trac/wiki/VCLExampleNormalizeAcceptEncoding
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm
remove req.http.Accept-Encoding;
}
}
# allow PURGE from localhost and 192.168.55...
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
if (req.request == "BAN") {
ban("obj.http.x-url ~ " + req.http.x-ban-url +
" && obj.http.x-host ~ " + req.http.x-ban-host);
error 200 "Banned";
}
if (req.request == "REFRESH") {
set req.request = "GET";
set req.hash_always_miss = true;
}
if (req.http.host ~ "pgadmin.infalms.com.br" || req.url ~ "admin" || req.http.host ~ "mediatarget.com.br" || req.http.host ~ "www.mediatarget.com.br") {
return(pass);
}
}
sub vcl_fetch {
if (beresp.ttl > 0s) {
/* Remove Expires from backend, it's not long enough */
unset beresp.http.expires;
/* Set the clients TTL on this object */
set beresp.http.cache-control = "max-age=900";
/* Set how long Varnish will keep it */
set beresp.ttl = 1w;
/* marker for vcl_deliver to reset Age: */
set beresp.http.magicmarker = "1";
}
# static files always cached
if (req.url ~ "^/static") {
unset beresp.http.set-cookie;
return (deliver);
}
# pass through for anything with a session/csrftoken set
if (beresp.http.set-cookie ~ "sessionid" || beresp.http.set-cookie ~ "csrftoken") {
return (hit_for_pass);
} else {
return (deliver);
}
}
sub vcl_deliver {
if (resp.http.magicmarker) {
/* Remove the magic marker */
unset resp.http.magicmarker;
/* By definition we have a fresh object */
set resp.http.age = "0";
}
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "[hit] Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 404 "[miss] Not in cache.";
}
}
Varnish 的另一个问题是,有些图像没有被缓存并且根本没有加载。像这个:
http://pcmag.infalms.com.br/media/oldsite_imgs/bodytext_img_862920.jpg
当我尝试清除它时,它会返回 [404]Miss。如果我一直按 F5,它有时会加载,但它完全是随机的。