Varnish Cache Configuration
Cache exclusions and VCL, checked before they go live
Manage the Varnish page cache
When Varnish is installed on your server, its card on the Services tab has a Configure button. It opens two tabs: Cache Exclusions for everyday rules, and Edit Raw VCL for everything else. Both check your change before it goes live, so a typo cannot take your sites down.
01 · Cache Exclusions
Some pages must never come from a shared cache: carts, compare pages, anything that keeps a visitor's choices in cookies. Addresses that match a rule here always skip the cache: they go to your site with their cookies, and the answer is never stored.
- Type a regular expression in URL Pattern (regex) and, in the label field, why the rule exists.
- Click Add Rule.
- Type an address in Test a URL against your rules to see which rule it matches, as you type.
- Keep Also purge already-cached URLs matching new rules ticked, so the rule works at once.
- Click Save & Apply.
Patterns are matched against the path and query string, for example /products/compare?selected=1,2. They are not anchored: use ^ and $. Write a literal ? or . as \? or \.. Double quotes are not allowed.
| Pattern | Matches |
|---|---|
/compare(\?|$) | Any address ending in /compare, with or without a query string. |
^/api/ | Everything under /api/. |
\.pdf$ | Addresses ending in .pdf. |
The rules live in their own file, included from the main VCL. If that include line goes missing after a manual edit, it is put back at the next save.
02 · Edit Raw VCL
The full /etc/varnish/default.vcl, for cache lifetimes, grace mode, cookies and anything else VCL can do. Save & Apply:
- keeps a dated copy of the current file (the last 10);
- compiles the new VCL first: on an error you see the compiler's message and the running configuration stays;
- loads the new VCL without restarting Varnish and without emptying the cache.
03 · First-time setup
When Varnish is installed but not yet set up for a site, Edit Raw VCL opens with a guide in comments and a reference VCL to uncomment. The chain it sets up:
visitor → Apache :80/:443 (TLS) → Varnish 127.0.0.1:6081 → Apache 127.0.0.1:8080 → PHP-FPM
- Make Varnish listen on
127.0.0.1:6081. - Move the site's virtual host to
127.0.0.1:8080, keeping real visitor addresses in the logs and telling PHP when the request came over HTTPS. - Create a front virtual host on ports 80 and 443 that keeps the certificate and passes everything to Varnish.
- Uncomment and adapt the reference VCL, then Save & Apply.
The guide has the full examples for each step. A file with only comments does not compile, so the editor asks you to uncomment the VCL part first.
04 · Check it works
The reference VCL adds an X-Cache header to every answer:
curl -sI https://www.example.com/some-page | grep X-Cache
- MISS, then HIT on the next request: the page is cached.
- MISS every time: the address is excluded or cannot be cached. That is what you want for a page with an exclusion rule.
The Stats button on the Varnish card shows the hit rate, memory and evictions: see Cache stats.
Services tab
- Services tab
- Start, stop, restart
- PHP configuration
- Cache stats
- Varnish cache