How do I use Varnish to cache my Plone site?

original page

  1. Install a 'custom app listening on port' app in the control panel. You can name the app whatever you want, let's call it "varnish" for example. Make a note of the port number assigned to the app.

  2. Install a Plone 4.1.2 app in the control panel. Let's name the app "zope", and again make a note of the port number.

  3. Edit ~/webapps/zope/zinstance/buildout.cfg to make the following changes:

    • In the eggs section, add plone.recipe.varnish

    • In the parts section, add varnish-build and varnish

    • At the end of buildout.cfg, add the following (change VVVVV to your varnish port, and change ZZZZZ to your Zope port):


    recipe = zc.recipe.cmmi
    url = ${varnish:download-url}


    [varnish] recipe = plone.recipe.varnish daemon = ${buildout:parts-directory}/varnish-build/sbin/varnishd bind = backends = cache-size = 256M


  4. Run your buildout script. You'll need to set a TEMP environment variable to point to a temp directory in your buildout, so do it like this:


    cd ~/webapps/zope/zinstance
    mkdir tmp
    export TEMP=$PWD/tmp


  5. Start varnish and restart Zope:


    ./bin/instance restart


  6. Log into your Plone site as an admin, go to Site Setup > Add-ons and install the 'HTTP caching support' add-on.

  7. Go to Site Setup > Caching and make the following settings:

    • Global: enable caching and enable GZip compression checked
    • Caching proxies: enable purging checked, as your proxy (change VVVVVto your Varnish port)
    • Caching operations > Ruleset mappings: Set as desired. The various options (strong, moderate, etc) are explained in the docs
  8. Edit your site to serve the Varnish app instead of the Zope app. It's a good idea to set up a second site to serve the Zope app directly so that you can still get to your site if there's a problem with Varnish.

Once that's done, just wait a bit for the control panel changes to gel, and then enjoy your new blazingly-fast site. How fast, you say? Well, here's a simple ab benchmark made directly against Zope:

$ ab -n 100 -c 5 | grep "Requests per second"
Requests per second:    16.47 [#/sec] (mean)

And here's the same site, served through Varnish:

$ ab -n 100 -c 5 | grep "Requests per second"
Requests per second:    5099.70 [#/sec] (mean)

That's over 300x faster! So, what are you waiting for? :)





[kyholdings@web151 zinstance]$ ab -n 100 -c 5 | grep "Requests per second"
Requests per second:    1.11 [#/sec] (mean)

I'm following

I have the domains www.mydomain.example and mydomain.example added to a website mywebsite1 that points to the Varnish app. In the Zope app I have VHM mappings like these:


(I also have a website mywebsite2 with the domain myotherdomain.example that points directly to the Zope app; myotherdomain.example is not in the VHM Mappings, so I can access the ZMI directly).

Do you have any advice on whether or not to enable the option 'Virtual host rewriting takes place front of the caching proxy' in the section Site Setup > Caching > Caching Proxies
since it is not clear to me if/where host or URL rewriting takes place in my set-up.

Since you're using VHM, and VHM handles your virtual host rewriting, and VHM runs in Zope, and Zope runs behind Varnish, you should not enable the 'Virtual host rewriting takes place front of the caching proxy' option.