Answers.Body: <p>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 <a href="https://gist.github.com/aelvan/eba03969f91c1bd51c40" rel="nofollow">in this gist</a>. It is based on the <a href="https://github.com/mattiasgeniar/varnish-4.0-configuration-templates" rel="nofollow">Varnish 4.0 template made by Mattias Geniar</a> with some adjustments for Craft. </p>
  
  <p>It is NOT an exact port of the 3.x file you're referencing, so it's important to understand what it does. </p>
  
  <p>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:</p>
  
  <pre><code>{% set expiry = now|date_modify('+7 days') %}
  {% header "Cache-Control: max-age=" ~ (expiry.timestamp - now.timestamp) %}
  {% header "Pragma: cache" %}
  {% header "Expires: " ~ expiry.rfc1123() %}
  </code></pre>
  
  <p>You can add all kinds of logic to this, setting different cache duration based on entry section, entry age, custom fields, or whatever. </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <code>/admin</code> or <code>p=admin</code>.</p>
  
  <p>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.</p>
  
  <p>But comments and fixes are greatly appreciated. :)</p>
  
  <p>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. </p>
  
  <p><strong>UPDATE:</strong><br>
  My Varnish Purge plugin is now available <a href="https://github.com/aelvan/VarnishPurge-Craft" rel="nofollow">here</a>. Also, check out Josh Angell's CacheMonster plugin <a href="https://github.com/supercool/Cache-Monster" rel="nofollow">here</a> for a different take on the same problem.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-30T19:21:23.683Z
Answers.Text: 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!
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-16T01:46:11.740Z
Answers.Text: Have you made any headway with that plugin. Really anxious to try Varnish :)
Answers.UserDisplayName: Seán O'Grady
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-16T08:12:04.153Z
Answers.Text: 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.
Answers.UserDisplayName: André Elvan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T15:35:24.563Z
Answers.LastActivityDate: 2015-10-17T22:16:43.977Z
Answers.LastEditDate: 2015-10-17T22:16:43.977Z
Answers.UserDisplayName: André Elvan
Body: <p>Is there any vcl file to use with Craft? I saw <a href="https://craftcms.stackexchange.com/questions/2/how-do-i-configure-varnish-to-work-with-craft">here</a> 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</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-24T12:50:53.557Z
Comments.Text: What about using the 3.x? I think you can simply get to work by updating the functions etc. with the help of https://www.varnish-cache.org/docs/trunk/whats-new/upgrading.html
Comments.UserDisplayName: Fred Carlsen
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-24T15:21:40.010Z
Comments.Text: I will try and add this here if i can make it work.
Comments.UserDisplayName: mbalparda
Tags: <cache><varnish>
Title: Varnish 4.x and Craft
ThreadText: # Varnish 4.x and Craft
  
  (Asked by: username_0 on 2014-09-24)
  
  Is there any vcl file to use with Craft? I saw [here](https://craftcms.stackexchange.com/questions/2/how-do-i-configure-varnish-to-work-with-craft) 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
  
  **Tags:** cache, varnish
  
  > **Comments:**
  > **username_1**: What about using the 3.x? I think you can simply get to work by updating the functions etc. with the help of https://www.varnish-cache.org/docs/trunk/whats-new/upgrading.html  
  > **username_0**: I will try and add this here if i can make it work.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2014-09-25. Score: 10 | Accepted answer
  
  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](https://gist.github.com/aelvan/eba03969f91c1bd51c40). It is based on the [Varnish 4.0 template made by Mattias Geniar](https://github.com/mattiasgeniar/varnish-4.0-configuration-templates) 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](https://github.com/aelvan/VarnishPurge-Craft). Also, check out username_3's CacheMonster plugin [here](https://github.com/supercool/Cache-Monster) for a different take on the same problem.
  
  > **Comments:**
  > **username_4**: Have you made any headway with that plugin. Really anxious to try Varnish :)  
  > **username_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.  
  > **username_3**: 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!  
  
  ---
  
  