Varnish
This documentation needs work. See "Help improve this page" in the sidebar.
Description
Varnish is a reverse proxy caching solution and is frequently used with Drupal. Out of the box it respects HTTP headers and cache accordingly the request in lieu of Drupal anonymous cache (Internal page cache module).
Use the Purge module with Varnish purge module. The Varnish module is not Drupal 10 ready, see #3443907: [meta] Drupal 10 version roadmap.
Minimal setup
Varnish should be installed on a specific server that responses to your web site hostname, then set your web server as the Varnish's backend.
Varnish is provided with a default configuration (https://www.varnish-software.com/developers/tutorials/varnish-builtin-vc...) than could be altered with a configuration file, usually default.vcl.
Drupal reverse proxy declaration
As a reverse proxy, Varnish alters HTTP headers, so you must enable and setup Drupal's reverse proxy settings.
Documentation
Installation, Tutorial, User-guide & Reference could be found at https://varnish-cache.org/docs/
If you install Varnish, you could safely disable the Internal page cache module.
Purging
You may need to purge (or ban) cached content, it's usually done with curl request or the Varnish command line tool. Here's some basic examples:
# Command line (on the varnish machine)
varnishadm ban req.http.host == example.com '&&' req.url '~' '\\.png$'
# Invalidate all cache
curl -IX BAN <VARNISH_HOST:ADMIN_PORT>
# Invalidate all cache under a specific path and host
curl -IX BAN <HOST_VARNISH> -H "ban-url: ^/foo/" -H "ban-host: \.foo\.com$"
# BAN also accepts wildcard URLs, such as:
curl -IX BAN 'http://HOSTNAME-OR-IP-OF-VARNISH/.*projects.*'
# Invalidate all cache marked wîth a specific cache tags (see below)
curl -IX BAN 'http://HOSTNAME-OR-IP-OF-VARNISH/' -H 'Cache-Tags: media:3918 media_list'
Purging policies
The default policy to respect the origin server headers (Drupal ones) is not the optimal, it could lead to return cached version of a freshly modified content or on the opposite to never cache some resources.
Hence you should setup a purging policies:
- Use the Drupal cache tags headers, it's the configuration recommanded by Varnish: https://www.varnish-software.com/developers/tutorials/configuring-varnis..., it's based on the Purge & Generic HTTP Purger modules.
- Setup Varnish to cache you cacheable pages for x minutes : it's a simple setup that could easily remove the load from you web site without adding to much latency between cached & live versions. You could also add a long "grace period" (weeks) that allow Varnish to return stale content if you website is down or in maintenance mode.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion