Skip to content

Instantly share code, notes, and snippets.

@rjdp
Created February 4, 2017 00:02
Show Gist options
  • Save rjdp/6783d2628844ac8f212ffdf93773caa6 to your computer and use it in GitHub Desktop.
Save rjdp/6783d2628844ac8f212ffdf93773caa6 to your computer and use it in GitHub Desktop.
Varnish 4.x vcl config for django
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.url ~ "^/static" || (req.http.cookie !~ "sessionid" && req.http.cookie !~ "csrftoken")) {
unset req.http.Cookie;
}
if (req.http.upgrade ~ "(?i)websocket") {
return (pipe);
}
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 {
# unkown algorithm
unset req.http.Accept-Encoding;
}
}
if (req.http.host ~ "^example.com") {
return (synth (750, ""));
}
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
if (bereq.url ~ "^/static" || (beresp.http.set-cookie !~ "sessionid" && beresp.http.set-cookie !~ "csrftoken")) {
unset beresp.http.set-cookie;
return (deliver);
}
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}
sub vcl_synth {
if (resp.status == 750) {
set resp.status = 301;
set resp.http.Location = "http://www.example.com" + req.url;
return(deliver);
}
}
sub vcl_pipe {
if (req.http.upgrade) {
set bereq.http.upgrade = req.http.upgrade;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment