后台已经选择Varnish,首页偶尔HIT,商品页却永远显示 X-Magento-Cache-Debug: MISS。先用真正的匿名请求测试,浏览器里残留的PHPSESSID、customer_group或调试Cookie会改变结果。
建立干净基线
curl -sI https://example.com/product.html curl -sI https://example.com/product.html curl -sI -H 'Cookie:' https://example.com/product.html
连续第二次仍MISS,再看响应中的Age、X-Varnish、Cache-Control和Set-Cookie。如果每次匿名响应都新设PHPSESSID,自定义代码可能在可缓存页面过早启动Session。
查layout里的不可缓存块
grep -R 'cacheable="false"' app/code app/design vendor/*/*/view/frontend/layout -n
一个 cacheable="false"块会让整页退出FPC。购物车、客户名等个性化内容应使用private content/sections,通过AJAX更新;不要简单删掉属性,否则可能把用户数据缓存给别人。
Cookie从哪里来
在测试环境给 Set-Cookie调用加堆栈日志,或逐个禁用最近模块。常见原因是Observer读取customer session、模板直接调用session manager、A/B测试模块每次生成新Cookie。只有参与页面差异的少数Cookie才应进入Varnish hash。
检查VCL是否主动pass
搜索 return (pass)规则,确认没有把所有带query string、所有catalog路径或无关Cookie都pass。用varnishlog按请求过滤,观察VCL_call最终走PASS还是HASH。
varnishlog -g request -q 'ReqURL ~ "product.html"'
完成修复后,匿名请求第二次应HIT;登录客户的公共页面仍可HIT,价格和私有区块通过customer-data更新。最后用两个客户账号交叉测试,确保姓名、购物车和价格组不会串缓存。

