Skip to content

Instantly share code, notes, and snippets.

@jenlampton
Created October 22, 2022 17:30
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 jenlampton/3aa63de5a6067f6a0445cc0a12208994 to your computer and use it in GitHub Desktop.
Save jenlampton/3aa63de5a6067f6a0445cc0a12208994 to your computer and use it in GitHub Desktop.
# A simplified version of the Lullabot best-practices Varnish configuration as
# described at:
#
# http://www.lullabot.com/articles/varnish-multiple-web-servers-drupal
#
# This configuration only uses a single web server on the localhost, optimized
# for a single machine instead of multiple web servers.
#
vcl 4.0;
# Define the internal network subnet.
# These are used below to allow internal access to certain files while not
# allowing access from the public internet.
acl internal {
"127.0.0.0"/24;
}
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# Respond to incoming requests.
sub vcl_recv {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
# Do not cache these paths.
if (req.url ~ "^/status\.php$" ||
req.url ~ "^/update\.php$" ||
req.url ~ "^/admin/build/features" ||
req.url ~ "^/check_locale.*$" ||
req.url ~ "^/info/.*$" ||
req.url ~ "^/flag/.*$" ||
req.url ~ "^.*/ajax/.*$" ||
req.url ~ "^.*/ahah/.*$") {
return (pass);
}
# Pipe these paths directly to Apache for streaming.
if (req.url ~ "^/admin/content/backup_migrate/export") {
return (pipe);
}
# Do not allow outside access to cron.php, install.php, or php files in /info.
if (req.url ~ "^/(cron|install|/info/.*)\.php$" && !client.ip ~ internal) {
# Have Varnish throw the error directly.
return (synth(404, "Page not found."));
# Use a custom error page that you've defined in Drupal at the path "404".
# set req.url = "/404";
}
# Handle compression correctly. Different browsers send different
# "Accept-Encoding" headers, even though they mostly all support the same
# compression mechanisms. By consolidating these compression headers into
# a consistent format, we can reduce the size of the cache and get more hits.=
# @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
# If the browser supports it, we'll use gzip.
set req.http.Accept-Encoding = "gzip";
}
else if (req.http.Accept-Encoding ~ "deflate") {
# Next, try deflate if it is supported.
set req.http.Accept-Encoding = "deflate";
}
else {
# Unknown algorithm. Remove it and send unencoded.
unset req.http.Accept-Encoding;
}
}
# Always cache the following file types for all users.
if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js)(\?[a-z0-9]+)?$") {
unset req.http.Cookie;
}
# Remove all cookies that Drupal doesn't need to know about. ANY remaining
# cookie will cause the request to pass-through to Apache. For the most part
# we always set the NO_CACHE cookie after any POST request, disabling the
# Varnish cache temporarily. The session cookie allows all authenticated users
# to pass through as long as they're logged in.
if (req.http.Cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|NO_CACHE)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.Cookie == "") {
# If there are no remaining cookies, remove the cookie header. If there
# aren't any cookie headers, Varnish's default behavior will be to cache
# the page.
unset req.http.Cookie;
}
else {
# If there is any cookies left (a session or NO_CACHE cookie), do not
# cache the page. Pass it on to Apache directly.
return (pass);
}
}
}
# Routine used to determine the cache key if storing/retrieving a cached page.
sub vcl_hash {
# Include cookie in cache hash.
# This check is unnecessary because we already pass on all cookies.
# if (req.http.Cookie) {
# set req.hash += req.http.Cookie;
# }
}
# Code determining what to do when serving items from the Apache servers.
sub vcl_backend_response {
# Don't allow static files to set cookies.
if (bereq.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js)(\?[a-z0-9]+)?$") {
# beresp == Back-end response from the web server.
unset beresp.http.set-cookie;
}
# Cache 404 errors for 10 seconds.
if (beresp.status == 404) {
set beresp.ttl = 10s;
}
# Keep stale copies for a long, long time so they can be used on infrequent pages.
set beresp.grace = 30d;
}
# In the event of an error, show friendlier messages.
sub vcl_synth {
# Redirect to some other URL in the case of a homepage failure.
#if (req.url ~ "^/?$") {
# set resp.status = 302;
# set resp.http.Location = "http://backup.example.com/";
#}
# Otherwise redirect to the homepage, which will likely be in the cache.
set resp.http.Content-Type = "text/html; charset=utf-8";
synthetic ({"
<html>
<head>
<title>Page Unavailable</title>
<style>
body {
background: #F1F2F6;
font-family: Helvetica, Arial, sans-serif;
font-size: 20px;
text-align: center;
color: #36323A;
}
#page {
border: 1px solid #F1F2F6;
-moz-box-shadow: 0 2px 4px rgba(46,47,51,0.5);
-webkit-box-shadow: 0 2px 4px rgba(46,47,51,0.5);
box-shadow: 0 2px 4px rgba(46,47,51,0.5);
width: 600px;
margin: 100px auto 0;
background: #FFF;
}
#page-inner {
padding: 30px;
}
.site-name {
background-color: #2A3142;
color: #fff;
padding: 15px 30px;
}
.site-name span {
color: #20F0B3;
}
a, a:link, a:visited { color: #CCC; }
.error { color: #222; }
</style>
</head>
<body>
<div id="page">
<div class="site-name"><span>Online</span> MBA REPORT</div>
<div id="page-inner">
<h1 class="title">Page Unavailable</h1>
<p>The page you requested is temporarily unavailable.</p>
<div class="error">(Error "} + resp.status + " " + resp.reason + {")</div>
</div>
</div>
</body>
</html>
"});
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment