Please feel free to add your useful VCL snippets to the page.
This page holds (links to) (somewhat) useful VCL examples.
- The Default VCL with examples and a graphical overview of how a request goes through all the VCL hooks.
- Purging (also see the FAQ?)
- Using restarts to try multiple backends
- Using directors for load balancing
- Caching even when cookies are present
- Caching content for logged in users or content based on cookie settings
- How to cache things longer on Varnish than the client
- Removing some, but not all cookies
- Enable force-refresh from clients
- Redirect using VCL
- How to ignore cache headers from the backend
- Log to syslog using VCL and embedded C
- Useful bits if you are using pipe
- Adding a header indicating hit/miss & extended diagnostics
- Grace - Handling backend failures and expired objects gracefully
- Saint Mode - Holding off backend requests for a specific object for a while
- How to avoid hotlinking to your site
- An example showing how Varnish can change your web server signature
- Redirecting the browser to another hostname
- Normalizing req.url
- Setting TTL in Varnish based on a header
- Normalize Accept-Language header
- Normalize User-Agent header
- Strip Google Adwords "gclid" parameter from bought Analytics tagged traffic
- Set Expires header based on max-age in Cache-Control
- Using Varnish to masquerade another site as part of your own URL space, useful for cross-domain XMLHTTPRequest
- Avoiding race condition when having two Varnishes as backends for each other
- Explicit cache miss when using scripts to refresh/prewarm cache
- Using static regular expressions to munge an incoming HTTP request -Very useful if you want to apply the same varnish config to all sites on a server hosting many, many small sites.
- Fix up Vary by removing User-Agent
VCL Real World Examples
Below are full VCL's used already in live production systems.
These should only be used as a reference to help build your own VCL!
- Submitted by alexc - 25-09-2009 - varnish 2.0.4 stable
- Submitted by Niall Douglas - 21st Jan 2011 - NEEDS varnish v2.0.5 or later. This VCL has the following features:
- Designed to primarily cache Zope and Plone content.
- Gives an example of how to "mount" parts of other websites onto your own e.g. here I mount Google Finance and the RePeC economics database. VERY useful for AJAX programming!
- Uses cunningly designed statically compiled regular expressions to perform VirtualHostMonster? style auto-generation of VirtualHostBase? paths out of the HTTP headers (includes HTTPS support!). This saves you having to specify them manually for each and every site you host.
- Enables varnish to serve stale content up to 24h old if Zope has gone down (very useful for performing upgrades), but keeps grace at 20s if the Zope backend is healthy. Note that this bit particularly requires the If-None-Match ETag support in varnish v2.0.5 and later.
- Patches SVGZ files to be returned as a more compatible SVG + gzip content encoded.
- Harmonises Content-Encoding to avoid caching too many copies.
- Submitted by Niall Douglas - 21st Jan 2011 - varnish v2.0.5 or later. This VCL configures a dumb content caching server which provides a local copy of another server's content. VERY useful for geo-targeted content, or for setting up an "everything has gone tits up so fall back to something" solution.
- Security.vcl - a Web Application Firewall implemented in VCL
- Varnish 3.0 templates - Wordpress, Drupal, Joomla and Fork CMS VCL templates
VCL Example Templates
Community supplied VCL templates to cover a few real world scenarios. These are meant to be a good starting point that can be tweaked to meet your own site requirements.
- Wordpress without a plugin that supports Purging - (Varnish 3.x) used if you are running Wordpress, but do not have a plugin with Purge support
- Wordpress with a plugin that supports Purging - (Varnish 3.x) used if you are running Wordpress with one of the plugins that does support purging
- Wordpress with a plugin that supports Purging and ESI - (Varnish 3.x) used if you are running Wordpress with a plugin that supports ESI and purging
- Drupal VCL