6

Is there any vcl file to use with Craft? I saw here an amazing answer about Varnish and Craft and there is a 3.x configuration file but i need a newer one for Varnish 4 and i don’t know Varnish Configuration Language to port it myself

10
+100

I'm by no means a Varnish pro, but I'm currently working on a project where we're running Varnish 4.0 in front of our nginx servers. I've uploaded an example of our VCL 4.0 file in this gist. It is based on the Varnish 4.0 template made by Mattias Geniar with some adjustments for Craft.

It is NOT an exact port of the 3.x file you're referencing, so it's important to understand what it does.

First of all, the 3.x file sets the ttl of cacheable requests in the VCL. Not sure if this is because it's harder to set the correct headers in ExpressionEngine, but I prefer to leave this to the backend server. If you don't set expire headers in your templates your pages won't be cached. To set a page to expire in 7 days you add this to your template:

{% set expiry = now|date_modify('+7 days') %}
{% header "Cache-Control: max-age=" ~ (expiry.timestamp - now.timestamp) %}
{% header "Pragma: cache" %}
{% header "Expires: " ~ expiry.rfc1123() %}

You can add all kinds of logic to this, setting different cache duration based on entry section, entry age, custom fields, or whatever.

In the 3.0 example file, all asset folders (like /scripts, /assets/, /files, etc) are excluded from caching. This is probably a good idea in most cases, see the comments from the original 4.0 template regarding this. But, in my project all content files are uploaded to Amazon S3, so the only assets served by nginx is css, js, font files, and some small assets. I've opted to just let Varnish handle everything for now.

I'm taking a heavy handed approach to cookies, because I don't need them, and I had some problems removing the Craft ones. I've left in some of the original stuff from the template, mostly for reference.

All requests to the Craft admin is passed on to the backend server. I had some issues with just excluding /admin, because the control panel sometimes add index.php to the url, so I'm excluding everything that contains /admin or p=admin.

All in all, this might not be a perfect VCL file, but it seems to work fine. Running Varnish on a DigitalOcean 1GB/1CPU droplet, I've cut the response time on my test page from ~1.5s (unoptimized/uncached template with a lot of matrix going on) to 40ms. Static tests with ApacheBench within the local network shows Varnish delivering ~900 requests/sec. Pretty awesome, me thinks.

But comments and fixes are greatly appreciated. :)

I'm also working on a Craft plugin for purging URLs when entries are changed, but it's not working very well at the moment.

UPDATE:
My Varnish Purge plugin is now available here. Also, check out Josh Angell's CacheMonster plugin here for a different take on the same problem.

|improve this answer|||||
  • Have you made any headway with that plugin. Really anxious to try Varnish :) – Sean O'Grady Dec 16 '14 at 1:46
  • 2
    Yes! I haven't had the time to polish it and put it up on github yet, but it works pretty good now. If you send me an email, I'll send you what I got, andre[at]vaersaagod.no. – André Elvan Dec 16 '14 at 8:12
  • Would I be able to partake in this plugin you've got going on? Got a site that I need to varnish but can't do it until I can bust it .... happy to contribute to the code if it helps and if I can! – Josh Angell Apr 30 '15 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.