Rate this article :
This article was useful to you ?
Yes
No
Vous avez noté 0 étoile(s)
Sommaire
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.
LWS shared hosting and managed cPanel/VPS packages are based on the :
Browser ⇄ NGINX (SSL + HTTP/2) ⇄ Varnish Cache ⇄ Apache/PHP-FPM
When everything is correctly configured, Varnish can deliver up to 1000 times faster than direct access to PHP, while relieving the server's CPU.
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! |
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"
Putting the same .htaccess file in the "/images/" folder applies the rule only to the "images" folder.
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.
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 |
curl -X PURGE -H "Host: example.com" https://exemple.com/chemin/ressource.jpg
ban req.http.host == "example.com" && req.url ~ "/images/"
curl -I https://exemple.com/style.css
Look at :
With Chrome/Edge: DevTools > Network > Disable cache can simulate a first visitor.
✅ Your Varnish cache is now under control!
Rate this article :
This article was useful to you ?
Yes
No
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