Skip to content

Instantly share code, notes, and snippets.

@zerolab
Created May 7, 2014 08:48
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 zerolab/7a4304f6426ee4b310b1 to your computer and use it in GitHub Desktop.
Save zerolab/7a4304f6426ee4b310b1 to your computer and use it in GitHub Desktop.
# Respond to incoming requests.
sub vcl_recv {
# ...code from above.
# 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 are any cookies left (a session or NO_CACHE cookie), do not
# cache the page. Pass it on to Apache directly.
return (pass);
}
}
}
@zerolab
Copy link
Author

zerolab commented May 7, 2014

Via https://www.lullabot.com/blog/article/configuring-varnish-high-availability-multiple-web-servers

The cookie snippet removes all spaces from cookies, then adds them for allowed cookies, then removes all cookies having no space before the ; and removes any prefixed cookies. So instead of whitelisting cookies, it is removing all besides SESS* and NO_CACHE cookies.

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