场景:CDN的作用在于就近缓存,缓存命中情况下对应资源的访问速度会相对较快,加速效果较好;如果CDN缓存命中率差,对应的CDN加速效果是相对不理想的,故提升CDN缓存命中率对于CDN加速效果有着重大影响,本文旨在让读者可以根据案例定位对应资源为何缓存不命中,如何才能缓存命中提高缓存命中率。
案例一:源站设置了no cache,private
(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设置
案例二:cdn设置对应规则缓存时间为0
curl -I http://dccdn.***.cn/1.jpg
HTTP/1.1 200 OK
Server: Tengine
Content-Type: image/jpeg
Content-Length: 277485
Connection: keep-alive
Date: Wed, 28 Sep 2016 12:51:39 GMT
x-oss-request-id: 57EBBCDB95B328E7E170F7EE
Accept-Ranges: bytes
ETag: "6B17A7CF2B799761F189D1D52011FDE2"
Last-Modified: Wed, 28 Sep 2016 12:48:57 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 12355230545077291646
Cache-Control: max-age=80
Content-MD5: axenzyt5l2HxidHVIBH94g==
x-oss-server-time: 45
Via: cache18.l2et15[60,200-0,M], cache9.l2et15[81,0], kunlun9.cn192[110,200-0,M], kunlun9.cn192[147,0]
X-Cache: MISS TCP_MISS dirn:-2:-2
X-Swift-SaveTime: Wed, 28 Sep 2016 12:51:39 GMT
X-Swift-CacheTime: 0
Timing-Allow-Origin: *
EagleId: 3adad78914750670995691589e
排查:
1)源站未设置nocache private等;
2)源站存在Cache-Control缓存头
3)查CDN缓存规则配置:
问题定位:cdn配置了缓存时间为0
解决:取消该cdn缓存规则的配置
案例三:cdn 未设置缓存规则,并且源站也未抛出与缓存相关的头如cache control,Expires,(Last-Modified/If-Modified-Since, Etag/If-None-Match要配合Cache-Control使用)等
解决:cdn设置缓存规则;
建议: 建议设置目录缓存 / ,权重设置的低一点,来保证全站缓存
参考:https://help.aliyun.com/document_detail/27136.html?spm=5176.doc27140.6.139.ddB4Tw
案例四:源站设置:cache-control:max-age=0
解决:取消源站的max-age=0,cdn设置缓存规则;
建议: 建议设置目录缓存 / ,权重设置的低一点,来保证全站缓存
参考:https://help.aliyun.com/document_detail/27136.html?spm=5176.doc27140.6.139.ddB4Tw
案例五:cdn配置缓存规则,源站未设置nocache,private;Vary: Accept-Encoding, Cookie导致缓存不能命中,注意:这边x-cache 是有对应时间的,但是每次访问还是miss的
排查:
1)源站未设置nocache,private;
2)源站存在cache-control等头;
3)cdn未设置缓存规则:缓存时间为0;
4)cdn返回的response头包含了 X-Swift-CacheTime:7200
5)源站返回的vary头包含cookie每次刷新cookie的值都在变,所以cache命中不了
解决:取消源站的Vary: Accept-Encoding, Cookie