Varnish was installed and configured, but every request was hitting PHP – hit rate was 0%. The server was overloaded despite Varnish. The problem was in wrong VCL configuration and session cookies. Fix time: 4 hours.
Symptoms
- Varnish running (port 6081), nginx listening on 8080
varnishstatshows MAIN.cache_hit = 0- Response header:
X-Cache: MISSon every request - PHP server overloaded despite Varnish
Cause
Magento was sending a PHPSESSID cookie on pages that should be cached. Varnish by default does not cache requests with cookies. The VCL also lacked a rule to strip PHP session cookies on static pages.
Solution
# Generate correct VCL from Magento bin/magento varnish:vcl:generate --export-version=6 --output-file=/etc/varnish/default.vcl # Reload Varnish with new VCL varnishadm vcl.load m2 /etc/varnish/default.vcl varnishadm vcl.use m2
sub vcl_recv {
# Remove cookies for static assets
if (req.url ~ "\.(jpg|jpeg|png|gif|css|js|ico|woff)$") {
unset req.http.Cookie;
return (hash);
}
# Strip non-essential cookies
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie,
"PHPSESSID=[^;]+;?\s*", "");
if (req.http.Cookie == "") {
unset req.http.Cookie;
}
}
}
Result
Hit rate after fix: 94%. PHP-FPM load: dropped from 80% to 8%. TTFB on home page: from 2.1s to 0.04s.
