Skip to content

Instantly share code, notes, and snippets.

@reifman
Last active September 9, 2021 08:15
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save reifman/4651531 to your computer and use it in GitHub Desktop.
Save reifman/4651531 to your computer and use it in GitHub Desktop.
Example Varnish VCL Configuration e.g. /etc/varnish/default.vcl
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 60s;
.first_byte_timeout = 60s;
.between_bytes_timeout = 60s;
.max_connections = 800;
}
acl purge {
"127.0.0.1";
"localhost";
}
sub vcl_recv {
set req.grace = 2m;
# Set X-Forwarded-For header for logging in nginx
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# Remove has_js and CloudFlare/Google Analytics __* cookies and statcounter is_unique
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js|is_
unique)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
# Don't cache, pass to backend
return (pass);
}
# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "")
;
# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?"
, "");
# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(;
)?", "");
# Static content unique to the theme can be cached (so no user uploaded images)
# The reason I don't take the wp-content/uploads is because of cache size on bigger blogs
# that would fill up with all those files getting pushed into cache
if (req.url ~ "wp-content/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") {
unset req.http.cookie;
}
# Even if no cookies are present, I don't want my "uploads" to be cached due to their potential size
if (req.url ~ "/wp-content/uploads/") {
return (pass);
}
# any pages with captchas need to be excluded
if (req.url ~ "^/contact/" || req.url ~ "^/links/domains-for-sale/")
{
return(pass);
}
# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
# A wordpress specific cookie has been set
return (pass);
}
# allow PURGE from localhost
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
# Force lookup if the request is a no-cache request from the client
if (req.http.Cache-Control ~ "no-cache") {
return (pass);
}
# Try a cache-lookup
return (lookup);
}
sub vcl_fetch {
#set obj.grace = 5m;
set beresp.grace = 2m;
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}
@DevinWalker
Copy link

I'm getting Syntax errors on restart of varnish using the file above. As mentioned, I think there are some line-breaks throwing me off...

@reifman
Copy link
Author

reifman commented Apr 17, 2013

I've fixed a few of the line breaks to make it easier for peeps.

@artasbartas
Copy link

Lines 26, 42, and 46 were the problem

@JREAM
Copy link

JREAM commented Apr 5, 2015

Here is a fix I did using VIM and 4 space indents only:
https://gist.github.com/JREAM/e16d5402fa536b14b43c

@uhlhosting
Copy link

Does this works with Apache too?

@abhijitrs
Copy link

  • On line 25, their is line break after is_
  • On line 45, there is line break after "wordpress_test_cookie=[^;]+(;

Remove these two and works with Apache

@clemenspeters
Copy link

How are POST requests handled with these settings?

shouldn't there exist something like


Do not cache example.com, the admin area,

logged-in users or POST requests

if (req.http.host ~ "example.com" ||
req.url ~ "^/admin" ||
req.http.Cookie ~ "logged_in" ||
req.request == "POST")
{
return (pass);

}


inside the vcl_recv block?
( I found this on https://www.linode.com/docs/websites/varnish/getting-started-with-varnish-cache)

@bhupendra-github
Copy link

Thanks a lot clemenspeters you saved me from a lots of headache.I added below code hope it helps someone else
if (req.request == "POST")
{
return (pass);
}
Also thanks a lot for JREAM and reifman

@donwilson
Copy link

donwilson commented Jun 25, 2020

If you're using Varnish 4.0+, all instances of req.request should be changed to req.method and vcl_fetch should be changed to vcl_backend_response (among perhaps other things). Per Upgrading to 4.0.

Edit: There's so much different between pre-4.0 and 4.0+ that it's best to just look at the documentation provided for purging cache in your VCL.

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