Varnish default.vcl grace period
- by Vladimir
These are my settings for a grace period (/etc/varnish/default.vcl)
sub vcl_recv {
....
set req.grace = 360000s;
...
}
sub vcl_fetch {
...
set beresp.grace = 360000s;
...
}
I tested Varnish using localhost and nodejs as a server. I started localhost, the site was up. Then I disconnected server and the site got disconnected in less than 2 min. It says:
Error 503 Service Unavailable Service Unavailable Guru Meditation: XID: 1890127100 Varnish cache server
Could you tell me what could be the problem?
sub vcl_fetch {
if (beresp.ttl < 120s) {
##std.log("Adjusting TTL");
set beresp.ttl = 36000s; ##120s;
}
# Do not cache the object if the backend application does not want us to.
if (beresp.http.Cache-Control ~ "(no-cache|no-store|private|must-revalidate)") {
return(hit_for_pass);
}
# Do not cache the object if the status is not in the 200s
if (beresp.status >= 300) {
# Remove the Set-Cookie header
#remove beresp.http.Set-Cookie;
return(hit_for_pass);
}
#
# Everything below here should be cached
#
# Remove the Set-Cookie header
####remove beresp.http.Set-Cookie;
# Set the grace time
## set beresp.grace = 1s; //change this to minutes in case of app shutdown
set beresp.grace = 360000s; ## 10 hour - reduce if it has negative impact
# Static assets - browser caches tpiphem for a long time.
if (req.url ~ "\.(css|js|.js|jpg|jpeg|gif|ico|png)\??\d*$") {
/* 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 = "public, max-age=31536000";
/* marker for vcl_deliver to reset Age: */
set beresp.http.magicmarker = "1";
}
else {
set beresp.http.Cache-Control = "private, max-age=0, must-revalidate";
set beresp.http.Pragma = "no-cache";
}
if (req.url ~ "\.(css|js|min|)\??\d*$") {
set beresp.do_gzip = true;
unset beresp.http.expires;
set beresp.http.cache-control = "public, max-age=31536000";
set beresp.http.expires = beresp.ttl;
set beresp.http.age = "0";
}
##do not duplicate these settings
if (req.url ~ ".css") {
set beresp.do_gzip = true;
unset beresp.http.expires;
set beresp.http.cache-control = "public, max-age=31536000";
set beresp.http.expires = beresp.ttl;
set beresp.http.age = "0";
}
if (req.url ~ ".js") {
set beresp.do_gzip = true;
unset beresp.http.expires;
set beresp.http.cache-control = "public, max-age=31536000";
set beresp.http.expires = beresp.ttl;
set beresp.http.age = "0";
}
if (req.url ~ ".min") {
set beresp.do_gzip = true;
unset beresp.http.expires;
set beresp.http.cache-control = "public, max-age=31536000";
set beresp.http.expires = beresp.ttl;
set beresp.http.age = "0";
}
## If the request to the backend returns a code other than 200, restart the loop
## If the number of restarts reaches the value of the parameter max_restarts,
## the request will be error'ed. max_restarts defaults to 4. This prevents
## an eternal loop in the event that, e.g., the object does not exist at all.
if (beresp.status != 200 && beresp.status != 403 && beresp.status != 404) {
return(restart);
}
if (beresp.status == 302) {
return(deliver);
}
# Never cache posts
if (req.url ~ "\/post\/" || req.url ~ "\/submit\/" || req.url ~ "\/ask\/" || req.url ~ "\/add\/") {
return(hit_for_pass);
}
##check this setting to ensure that it does not cause issues for browsers with no gzip
if (beresp.http.content-type ~ "text") {
set beresp.do_gzip = true;
}
if (beresp.http.Set-Cookie) {
return(deliver);
}
##if (req.url == "/index.html") {
set beresp.do_esi = true;
##}
## check if this is needed or should be used
# return(deliver); the object
return(deliver);
}
sub vcl_recv {
##avoid leeching of images
call hot_link;
set req.grace = 360000s; ##2m
## if one backend is down - use another
if (req.restarts == 0) {
set req.backend = cache_director; ##we can specify individual VMs
} else if (req.restarts == 1) {
set req.backend = cache_director;
}
## post calls should not be cached - add cookie for these requests if using micro-caching
# Pass requests that are not GET or HEAD
if (req.request != "GET" && req.request != "HEAD") {
return(pass); ## return(pass) goes to backend - not cache
}
# Don't cache the result of a redirect
if (req.http.Referer ~ "redir" || req.http.Origin ~ "jumpto") {
return(pass);
}
# Don't cache the result of a redirect (asking for logon)
if (req.http.Referer ~ "post" || req.http.Referer ~ "submit" || req.http.Referer ~ "add" || req.http.Referer ~ "ask") {
return(pass);
}
# Never cache posts - ensure that we do not use these strings in our URLs' that need to be cached
if (req.url ~ "\/post\/" || req.url ~ "\/submit\/" || req.url ~ "\/ask\/" || req.url ~ "\/add\/") {
return(pass);
}
## if (req.http.Authorization || req.http.Cookie) {
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}
# Handle compression correctly. Different browsers send different
# "Accept-Encoding" headers, even though they mostly all support the same
# compression mechanisms. By consolidating these compression headers into
# a consistent format, we can reduce the size of the cache and get more hits.
# @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
}
else if (req.http.Accept-Encoding ~ "gzip") {
# If the browser supports it, we'll use gzip.
set req.http.Accept-Encoding = "gzip";
}
else if (req.http.Accept-Encoding ~ "deflate") {
# Next, try deflate if it is supported.
set req.http.Accept-Encoding = "deflate";
}
else {
# Unknown algorithm. Remove it and send unencoded.
unset req.http.Accept-Encoding;
}
}
# lookup graphics, css, js & ico files in the cache
if (req.url ~ "\.(png|gif|jpg|jpeg|css|.js|ico)$") {
return(lookup);
}
##added on 0918 - check if it causes issues with user specific content
if (req.request == "GET" && req.http.cookie) {
return(lookup);
}
# Pipe requests that are non-RFC2616 or CONNECT which is weird.
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
##closing connection and calling pipe
return(pipe);
}
##purge content via localhost only
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
## do we need this?
## return(lookup);
}