Force Varnish caching on LWS

Procédure

In this documentation, we will explain step-by-step how to force Varnish and browsers to keep your resources cached, even when they send Pragma: no-cache or Cache-Control: no-cache requests.

1. Context and LWS architecture

LWS shared hosting and managed cPanel/VPS packages are based on the :

Browser ⇄ NGINX (SSL + HTTP/2) ⇄ Varnish Cache ⇄ Apache/PHP-FPM

  • NGINX manages TLS and compresses content.
  • Varnish (Fastest Cache) is used as a reverse proxy to store HTTP responses.
  • Apache or PHP-FPM generate the final response when Varnish has no HIT.

When everything is correctly configured, Varnish can deliver up to 1000 times faster than direct access to PHP, while relieving the server's CPU.

2. Reminder: how does Varnish decide to cache?

Element Impact on the cache How to influence it
Method Only GET and HEAD are eligible Avoid POSTs for public pages
Cache-Control, Expires, Pragmaresponse headers Determine duration and scope Set via .htaccess (see §3)
Cookies / Set-Cookie One cookie present = no caching by default Delete or disable unnecessary cookies
HTTP status 200, 203, 301, 302, 404, 410 are cacheable No action, but avoid 500!

[tips]Varnish ignores by default the Cache-Control: no-cache sent by the browser for static files, but respects the "revalidation logic" if the object is already cached[/tips].

3. Control the lifetime via .htaccess

Activate the mod_headers module (this is the default at LWS) then place the following snippet wherever you want:


  Header set Cache-Control "public, max-age=3600, s-maxage=3600, stale-while-revalidate=60, stale-if-error=86400" Header set Expires "Thu, 31 Dec 2037 23:55:55 GMT"
  • max-age: duration (in seconds) in the browser.
  • s-maxage: specific duration for shared caches (Varnish/CDN).
  • stale-while-revalidate: allows Varnish to serve stale content while it refreshes it in the background.
  • stale-if-error: serves the out-of-date version if the backend responds in error (fail-safe).

3.1 Targeting by folder

Putting the same .htaccess file in the "/images/" folder applies the rule only to the "images" folder.

3.2 Targeting by extension


  
    Header set Cache-Control "public, max-age=2592000, s-maxage=2592000, immutable".  

immutable: tells the browser that no revalidation is required until the object has expired; ideal for versioned files (style.483bf.css).

3.3 Short cache for HTML


  Header set Cache-Control "public, max-age=300, s-maxage=600, must-revalidate".

Expires after 5 min client-side and 10 min Varnish-side, then must-revalidate.

4. Real-life usage scenarios

Real case Fragment of .htaccess Why would it do this?
Landing page updated every hour max-age=600, s-maxage=1200 Visitors get "near live" data without overloading PHP
Versioned CSS/JS max-age=31536000, immutable Virtually no server traffic, instant loading
E-commerce product images max-age=604800 Reduces TTFB, speeds up the catalogue
Back-office / wp-admin no-store, private Avoids putting sensitive data in the shared cache

5. Purge or invalidate the cache

  1. From the LWS Panel: Optimisation & Performance > LWS Cache > Empty cache.
  2. HTTP PURGE (if enabled):
    curl -X PURGE -H "Host: example.com" https://exemple.com/chemin/ressource.jpg
  3. Ban by expression (advanced VCL):
    ban req.http.host == "example.com" && req.url ~ "/images/"

6. Testing and debugging

curl -I https://exemple.com/style.css

Look at :

  • Age: 356 → Varnish served a response aged 356 s.
  • Cache-Control: public, max-age=2592000, s-maxage=2592000, immutable
  • X-Cache: HIT (or MISS).

With Chrome/Edge: DevTools > Network > Disable cache can simulate a first visitor.

7. Common pitfalls

  1. Unnecessary cookies: many WordPress themes set a cookie even for public pages. Delete them or disable them via functions.php.
  2. Query strings: by default, ?v=123 creates a new entry in the cache. Version in the file name instead.
  3. Sensitive content: forms, customer area pages ➡ no-store, private, max-age=0.
  4. Brotli/Gzip compression: activate them before the Varnish layer to avoid double caching.

8. Summary of best practices

  • Use s-maxage ⩾ max-age to keep a proxy cache that is longer than the browser cache.
  • Put immutable on any file whose name contains a hash.
  • Do not mix Obsolete Expires with max-age except for older browsers.
  • Document your TTLs in a CACHE_POLICY.md file for the team.

9. Useful resources

✅ Your Varnish cache is now under control!

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

1mn reading

How can I access site visit statistics?

1mn reading

How do I activate Mod_PageSpeed on my site?

1mn reading

How do I use the cache modules on LWSPanel?

3mn reading

Speed up your site with LWS Cache


Ask the LWS team and its community a question