Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Forked from ethier/default.vcl.pl
Last active August 29, 2015 13:55
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 seyhunak/8715786 to your computer and use it in GitHub Desktop.
Save seyhunak/8715786 to your computer and use it in GitHub Desktop.
Varnish 3 - VCL
# The initial was for Varnish 2.1, this has been updated for Varnish 3.
# The upgrade changes were based on the docs here:
# https://www.varnish-cache.org/docs/3.0/installation/upgrade.html
# https://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
# https://www.varnish-cache.org/trac/wiki/VCLExamples
# Summary
# 1. Varnish will poll the backend at /health_check to make sure it is
# healthy. If the backend goes down, varnish will server stale content
# from the cache for up to 1 hour.
# 2. Varnish will pass X-Forwarded-For headers through to the backend
# 3. Varnish will remove cookies from urls that match static content file
# extensions (jpg, gif, ...)
# 4. Varnish will normalize the Accept-Encoding header
# 5. Varnish will respect the Cache-Control header, even when a Set-Cookie
# header accompanies it from the backend. For example,
# `Cache-Control: no-cache` will not get stored in Varnish and
# `Cache-Control: max-age=600` will.
# 6. Varnish will add a X-Varnish-Cache header to aid in debugging
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
}
# NOTE: vcl_recv is called at the beginning of a request, after the complete
# request has been received and parsed. Its purpose is to decide whether or not
# to serve the request, how to do it, and, if applicable, which backend to use.
sub vcl_recv {
set req.grace = 1h;
# 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") {
return (pipe);
}
# Don't cache POST, PUT, or DELETE requests
if (req.request == "POST" || req.request == "PUT" || req.request == "DELETE") {
return(pass);
}
# Do not cache if HTTP authorized.
if (req.http.Authorization) {
return (pass);
}
# Always cache things with these extensions.
if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$") {
unset req.http.cookie;
return (lookup);
}
# Skip the Varnish cache for logged users.
if (req.url ~ "dashboard|settings") {
return (pass);
}
# Strip cookies from static content
if (req.request == "GET" && req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
unset req.http.cookie;
}
# We will try to retrieve every request from the cache. There will be no
# intelligence on the varnish side to determine whether to look or not look
# at the cache.
return(lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
set bereq.http.connection = "close";
return(pipe);
}
# NOTE: vcl_fetch is called after a document has been successfully retrieved
# from the backend. Normal tasks her are to alter the response headers, trigger
# ESI processing, try alternate backend servers in case the request failed.
sub vcl_fetch {
if (beresp.status == 200) {
set beresp.ttl = 1h;
return (deliver);
}
if (beresp.ttl <= 0s ||
beresp.http.set-cookie ||
beresp.http.Vary == "*") {
set beresp.ttl = 120s;
return (hit_for_pass);
}
if (beresp.status != 200) {
set beresp.ttl = 30s;
}
# If header specifies "no-cache", don't cache.
if (
beresp.http.Pragma ~ "no-cache" ||
beresp.http.Cache-Control ~ "no-cache" ||
beresp.http.Cache-Control ~ "private"
) {
return(hit_for_pass);
}
# Do not deliver into cache otherwise.
return(deliver);
}
sub vcl_error {
if (obj.status >= 500 && obj.status <= 505) {
set obj.grace = 1h;
return(restart);
}
return (deliver);
}
sub vcl_deliver {
# The below provides custom headers to indicate whether the response came from
# varnish cache or directly from the app.
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Varnish-Cache = "MISS";
}
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Not in cache";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment