Skip to content

Instantly share code, notes, and snippets.

@dundee
Created January 31, 2013 10:19
Show Gist options
  • Save dundee/4681935 to your computer and use it in GitHub Desktop.
Save dundee/4681935 to your computer and use it in GitHub Desktop.
Varnish configuration
acl local {
"localhost";
}
backend default {
.host = "127.0.0.1";
.port = "80";
.first_byte_timeout = 3s;
}
backend fake {
.host = "127.0.0.1";
.port = "81";
.probe = {
.timeout = 0.1s;
}
}
sub vcl_recv {
# Handle timeout from backend
if ( req.http.magicmarker && req.http.magicmarker == "fake" ) {
unset req.http.magicmarker;
set req.backend = fake;
} else {
set req.backend = default;
}
# Refresh cache on X-REFRESH header
if (req.http.X-REFRESH && client.ip ~ local) {
set req.hash_always_miss = true;
}
# Serve stale data if needed
if (!req.backend.healthy) {
set req.grace = 5h;
} else {
set req.grace = 1h;
}
}
sub vcl_fetch {
# Process ESI
set beresp.do_esi = true;
# Do not overload backend if returns error
if (beresp.status >= 500) {
set beresp.saintmode = 10m; # try again after 10m
return(restart);
}
# Cache entry for 5 more hours than TTL
set beresp.grace = 5h;
return (deliver);
}
sub vcl_error {
# Handle timeout from backend
if (obj.status == 503 && req.restarts < 5) {
if (req.restarts == 0){
set req.http.magicmarker = "fake";
}
return(restart);
}
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<!DOCTYPE html>
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
</body>
</html>
"};
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment