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):

     

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

     

    [varnish] recipe = plone.recipe.varnish daemon = ${buildout:parts-directory}/varnish-build/sbin/varnishd bind = 127.0.0.1:VVVVV backends = 127.0.0.1:ZZZZZ 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
    ./bin/buildout

     

  5. Start varnish and restart Zope:

     

    ./bin/varnish
    ./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, http://127.0.0.1:VVVVV 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 plone.app.caching 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 http://zope.testweb183.webfactional.com/Plone/ | 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 http://testweb183.webfactional.com/Plone/ | 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 http://www.kyholdings.com/alex | grep "Requests per second"
Requests per second:    1.11 [#/sec] (mean)
 
 

I'm following

http://community.webfaction.com/questions/2263/how-do-i-use-varnish-to-cache-my-plone-site#fmanswer

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:

mydomain.example/myplonesite1
*.mydomain.example/myplonesite1

(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.