Skip to content

Instantly share code, notes, and snippets.

@johnscancella
Last active April 11, 2017 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnscancella/45cecd3c66fc065e2c1bc3a60d318c88 to your computer and use it in GitHub Desktop.
Save johnscancella/45cecd3c66fc065e2c1bc3a60d318c88 to your computer and use it in GitHub Desktop.
varnish config for chronam
# new 4.0 format.
vcl 4.0;
import directors;
backend app1 {
.host = "ndnpappvlp01.loc.gov";
.probe = {
.url = "/";
.interval = 60s;
.timeout = 10s;
.window = 5;
.threshold = 3;
}
}
backend app2 {
.host = "ndnpappvlp02.loc.gov";
.probe = {
.url = "/";
.interval = 60s;
.timeout = 10s;
.window = 5;
.threshold = 3;
}
}
#create a virtual group for use when we do the load balancing
sub vcl_init {
new app = directors.hash();
app.add_backend(app1, 1);
app.add_backend(app2, 1);
}
#stream all files, otherwise you have to wait for e.g. an entire PDF or JP2, etc. to be saved into the cache before Varnish will send the first byte to the client
sub vcl_backend_response {
set beresp.do_stream = true;
set beresp.grace = 1h;
if (beresp.http.content-type ~ "(text|application)") {
set beresp.do_gzip = true;
}
}
sub vcl_recv {
#load balance by the user's ip address
set req.backend_hint = app.backend(client.identity);
# unset cookies since we don't want to bypass caching normally
if (req.http.cookie) {
unset req.http.cookie;
}
#don't cache or modify /data or any pdf files
if (req.url ~ "^/data/") {
return (pass);
}
if (req.url ~ "\.pdf$") {
return (pass);
}
}
#prefer a fresh object, but when one cannot be found Varnish will look for stale one. This replaces req.grace in vcl_recv()
sub vcl_hit {
if (obj.ttl >= 0s) {
return (deliver);
}
if (obj.ttl + obj.grace > 0s) {
return (deliver);
}
return (fetch);
}
#vcl_deliver’s support for Vary headers needs to be preserved – at the very least we need the “Vary: Accept-Encoding” for anything which has gzip-compression enabled
sub vcl_deliver {
if (!resp.http.Vary) {
set resp.http.Vary = "Accept-Encoding";
} else if (resp.http.Vary !~ "(?i)Accept-Encoding") {
set resp.http.Vary = resp.http.Vary + ",Accept-Encoding";
}
}
#TODO but we should probably use a standard LC error page or confirm that CloudFlare will substitute the nice P1 error page for a backend 5xx error
sub vcl_backend_error {
set beresp.http.Content-Type = "text/html; charset=utf-8";
set beresp.http.Retry-After = "5";
synthetic ({"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>The page is temporarily unavailable</title>
</head>
<body>
<h1>Chronicling America is currently unavailable</h1>
<p>The Chronicling America website is currently offline, undergoing maintenance. We regret the inconvenience, and invite you to visit other collections available on the Library of Congress website at <a href="http://www.loc.gov">www.loc.gov</a> while we are working to restore service.</p>
</body>
</html>
"});
return (deliver);
}
@acdha
Copy link

acdha commented Mar 22, 2017

This looks like a good first pass. How are we handling purges (e.g. https://www.varnish-cache.org/docs/4.1/users-guide/purging.html) - is there any current attempt to purge Varnish when e.g. new content is loaded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment