场景:CDN的作用在于就近缓存,缓存命中情况下对应资源的访问速度会相对较快,加速效果较好;如果CDN缓存命中率差,对应的CDN加速效果是相对不理想的,故提升CDN缓存命中率对于CDN加速效果有着重大影响,本文旨在让读者可以根据案例定位对应资源为何缓存不命中,如何才能缓存命中提高缓存命中率。
(1)Cache control设置nocache, private
curl -I http://m.***.net/cha.html HTTP/1.1 200 OK Server: Tengine Content-Type: text/html; charset=utf-8 Content-Length: 13082 Connection: keep-alive Vary: Accept-Encoding Cache-Control: private X-AspNet-Version: 4.0.30319 Set-Cookie: ASP.NET_SessionId=zpt15ddhng0voq5o3qanfvrw; path=/; HttpOnly X-Powered-By: ASP.NET Date: Fri, 23 Sep 2016 10:49:33 GMT Via: cache15.l2cm12[20,200-0,M], cache4.l2cm12[20,0], kunlun8.cn180[88,200-0,M], kunlun6.cn180[89,0] X-Cache: MISS TCP_MISS dirn:-2:-2 X-Swift-SaveTime: Fri, 23 Sep 2016 10:49:34 GMT X-Swift-CacheTime: 0 Timing-Allow-Origin: * EagleId: 3ad8110614746277741253274e解决:取消源站cache contorl:nocache 或者private的设置
(2) Pragma 设置 nocache, private,标注:Pragma行是为了兼容HTTP1.0,作用与Cache-Control是一样的
curl -I http://srv.***.net/media/html/2016mzwhjxy/zhengce/sp.html HTTP/1.1 200 OK Server: Tengine Content-Type: text/html; charset=UTF-8 Content-Length: 258 Connection: keep-alive Pragma: no-cache Cache-Control: max-age=86400 Via: cache12.l2cm12[106,200-0,M], cache15.l2cm12[106,0], kunlun4.cn125[113,200-0,M], kunlun6.cn125[113,0] X-Cache: MISS TCP_MISS dirn:-2:-2 X-Swift-SaveTime: Thu, 01 Sep 2016 08:26:12 GMT X-Swift-CacheTime: 0 Timing-Allow-Origin: * EagleId: 6a78b50614727183720984671e解决:取消源站的Pragma: no-cache设置
排查:1)源站未设置nocache private等;2)源站存在Cache-Control缓存头3)查CDN缓存规则配置:
问题定位:cdn配置了缓存时间为0解决:取消该cdn缓存规则的配置
解决:cdn设置缓存规则;建议: 建议设置目录缓存 / ,权重设置的低一点,来保证全站缓存参考:https://help.aliyun.com/document_detail/27136.html?spm=5176.doc27140.6.139.ddB4Tw
解决:取消源站的max-age=0,cdn设置缓存规则;建议: 建议设置目录缓存 / ,权重设置的低一点,来保证全站缓存参考:https://help.aliyun.com/document_detail/27136.html?spm=5176.doc27140.6.139.ddB4Tw
排查:1)源站未设置nocache,private;2)源站存在cache-control等头;3)cdn未设置缓存规则:缓存时间为0;4)cdn返回的response头包含了 X-Swift-CacheTime:72005)源站返回的vary头包含cookie每次刷新cookie的值都在变,所以cache命中不了解决:取消源站的Vary: Accept-Encoding, Cookie
相关资源:七夕情人节表白HTML源码(两款)