Varnish provides high-performance HTTP caching that can dramatically improve Drupal site performance under load. This guide covers setting up Varnish specifically for a Drupal deployment.
Why Varnish for Drupal Specifically
See How to Optimize Performance for Drupal/Joomla on a VPS for general performance context — Drupal's page generation can be resource-intensive for complex sites; Varnish caches fully-rendered pages, serving them without hitting Drupal/PHP/database at all for cached requests, a significant performance multiplier for cacheable content.
Step 1 — Install Varnish
sudo apt install varnish -y
Step 2 — Configure Varnish to Sit in Front of Your Web Server
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Varnish sits between visitors and your actual web server (Nginx/Apache), which you'll reconfigure to listen on an internal port (8080 here) rather than the public-facing port.
Step 3 — Reconfigure Nginx/Apache to Listen Internally
listen 127.0.0.1:8080;
Your actual web server now only accepts connections from Varnish (localhost), not directly from the public internet — Varnish becomes the actual public-facing entry point.
Step 4 — Configure Varnish to Listen Publicly
DAEMON_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m"
Step 5 — Configure Drupal-Specific Caching Rules in VCL
sub vcl_recv {
if (req.http.Cookie ~ "SESS") {
return (pass);
}
unset req.http.Cookie;
}
Drupal sets session cookies for authenticated users — this rule ensures logged-in user requests bypass the cache (correctly seeing personalized content), while anonymous requests (no session cookie) are cached, the standard Drupal+Varnish caching pattern.
Step 6 — Configure Drupal's Cache Settings to Cooperate with Varnish
In Drupal's performance settings, ensure page caching is enabled and cache lifetime is configured appropriately — Drupal needs to send correct cache-control headers for Varnish to make appropriate caching decisions.
Step 7 — Handle Cache Invalidation on Content Updates
sub vcl_recv {
if (req.method == "PURGE") {
return (purge);
}
}
Configure Drupal's cache tag invalidation module to send purge requests to Varnish when content changes — ensures visitors see updated content promptly rather than stale cached pages persisting until natural expiration.
Testing That Caching Is Actually Working
curl -I https://yourdomain.com | grep X-Varnish
Verify Varnish headers appear in responses, confirming requests are genuinely passing through and being handled by Varnish as expected.
Monitoring Cache Hit Rate
varnishstat
Track cache hit ratio to understand how effectively your caching configuration is working — a low hit rate suggests either overly restrictive caching rules or genuinely high proportion of non-cacheable (authenticated/dynamic) traffic.
Common Errors
Authenticated users see other users' cached content — a serious caching bug; verify your VCL correctly excludes authenticated requests (session cookie present) from caching, since this specific mistake can cause genuine privacy/security issues.
Continue Reading
- How to Optimize Performance for Drupal/Joomla on a VPS
- How to Set Up a Reverse Proxy Cache to Reduce Origin Server Load
- How to Install Drupal on a VPS
Browse more articles in CMS Platforms Beyond WordPress.