Procédure
In this documentation, we will explain step by step how to force Varnish and browsers to keep your resources in cache, even when they send Pragma: no-cache or Cache-Control: no-cache requests.
The shared hosting and managed cPanel/VPS plans from LWS rely on the stack:
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 easing the server CPU load.
| Element | Impact on cache | How to influence it |
| Method | Only GET and HEAD are eligible | Avoid POST for public pages |
| Response headers Cache-Control, Expires, Pragma | Determine duration and scope | Configure via .htaccess (see §3) |
| Cookies / Set-Cookie | A present cookie = no caching by default | Remove or neutralize unnecessary cookies |
| HTTP status | 200, 203, 301, 302, 404, 410 are cacheable | No action required, but avoid 500s! |
Trucs et astuces By default, Varnish ignores the Cache-Control: no-cache sent by the browser for static files, but respects the "revalidation logic" if the object is already in cache.
Enable 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"
Placing 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 needed as long as the object has not 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 on the client side and 10 min on the Varnish side, then revalidation is mandatory.
| Real case | .htaccess fragment | Why? |
| Landing page updated every hour | max-age=600, s-maxage=1200 | Visitors get data that is “almost live” without saturating PHP |
| Versioned CSS/JS | max-age=31536000, immutable | Almost no traffic on the server, instant loading |
| E-commerce product images | max-age=604800 | Reduces TTFB, speeds up the catalog |
| Back office / wp-admin | no-store, private | Avoids storing sensitive data in the shared cache |
From your hosting environment (your site’s PHP script, Cron task, or SSH terminal), purging the Varnish cache does not require any token:
curl -X PURGE https://exemple.com/chemin/ressource.jpg
curl -X PURGE -H 'X-Purge-Method: regex' 'https://exemple.com/images/.*'
curl -X FULLPURGE https://exemple.com
$ch = curl_init('https://exemple.com/chemin/page.html');
curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => 'PURGE', CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 20]);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200 : cache purge ; 405 : purge refused
curl_close($ch);A complete script, ready for a Cron task, is available in the help article Speed up your site with Fastest Cache. You can also purge the cache from your dashboard, using the “Purge cache” button in Fastest Cache.
curl -I https://exemple.com/style.cssLook at:
With Chrome/Edge: DevTools > Network > Disable cache can simulate a first-time visitor.
✅ Your Varnish cache is now under control!
Ask the LWS team and the community. Answers are published after moderation.