Skip to content

Instantly share code, notes, and snippets.

@bcoe
Created April 25, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcoe/d58947885119ab22cc23d325c21b02fd to your computer and use it in GitHub Desktop.
Save bcoe/d58947885119ab22cc23d325c21b02fd to your computer and use it in GitHub Desktop.
ha.varnish
vcl 4.0;
import directors;
backend primary {
.host = "127.0.0.1";
.port = "8080";
}
backend replica {
.host = "x.x.x.x";
.port = "8080";
}
sub vcl_init {
new pkgread = directors.round_robin();
pkgread.add_backend(primary);
pkgread.add_backend(replica);
}
sub vcl_backend_response {
unset beresp.http.Etag;
if (bereq.url ~ "@" || beresp.status >= 300 || bereq.method != "GET") {
# DON'T CACHE SCOPED MODULES.
set beresp.ttl = 0s;
} else if (bereq.url ~ "\.tgz$") {
# CACHE TARBALLS FOR A LONG TIME.
set beresp.ttl = 21600s;
} else if (bereq.url ~ "^/[^/]+$") {
# DON'T CACHE JSON FOR LONG.
set beresp.ttl = 300s;
} else {
# DON'T CACHE SPECIAL ROUTES
set beresp.ttl = 0s;
}
}
sub vcl_hash {
hash_data(req.url);
return (lookup);
}
sub vcl_recv {
set req.http.X-Authorization = req.http.Authorization;
unset req.http.Authorization;
unset req.http.If-Modified-Since;
if (req.method == "GET") {
set req.backend_hint = pkgread.backend();
} else {
set req.backend_hint = primary;
}
}
sub vcl_backend_response {
if (beresp.status >= 300 && bereq.retries == 0 && bereq.method == "GET") {
return(retry);
}
}
sub vcl_backend_fetch {
set bereq.http.Authorization = bereq.http.X-Authorization;
if (bereq.retries > 0) {
set bereq.backend = primary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment