Varnish Cache

Last updated on
16 August 2016

Varnish Cache is a modern reverse proxy cache software package. It has been built from the ground up to be a dedicated reverse proxy cache so it doesn't have legacy functionality and code, making it blazing fast. It is supported on Linux and FreeBSD and is included in most popular distributions

Drupal Modules that interact with Varnish

  • Varnish Module integrates Drupal to Varnish over the Varnish administration interface. It can purge URLs from the proxy cache using regular expressions and can display varnish statistics.
  • Purge Module just implements purging over HTTP together with the Expire Module

Configuring Varnish for Drupal

Varnish has it's own highly flexible C-like configuration language, VCL. It allows a site administrator to highly control how HTTP objects are handled by Varnish and allows to highly optimize for very specific use cases. A VCL configuration file reflects the architecture of Varnish by implementing the different functions Varnish uses to process an HTTP request. One "feature" may be implemented on more then one of these functions. There are with each major Varnish release (2.0, 2.1, 3.0 etc) new features are added but sadly also some changes in the VCL syntax that break existing configurations.

Once installed Varnish ships with a default configuration file at /etc/varnish/default.vcl that should get you up and running with some very conservative settings. This handbook section contains example configuration snippets that are intended to help you understand to take better advantage of the power of Varnish and do some Drupal specific tuning. These should always be adapted to fit your specific Varnish version and site's needs.

Backends

If you're running a single site on a server, or else want all sites on a server to go through Varnish you'd only need one of the following backends. Showing different possibilities for those who have sites that they don't want to run Varnish on. In these examples, Varnish is assumed to be running on port 80, and Apache (or whatever) on port 8080.

Grace

Grace is a feature that allows Varnish to temporarily serve "old" content while it's fetching a new version. This greatly smooths the flow of traffic and improves average load times.

Request Sanitation

You can use VCL to modify request to improve consistency and thus cache-ability.

Purge

Implement the PURGE request as sent by the Purge module. To prevent DDOS attacks make sure you set an access list (ACL) for purge requests.

Passthrough

Sometimes you don't want Varnish to do anything to a request. You can specify exactly when and when not to cache or modify objects. You can also set extra headers indicating if Varnish was able to serve the request from cache or not. Very usefull for debugging.

Cookie Handling

Unused cookies are bad for your cache hit ratio and it's best to remove those you're sure you're not going to need.