原文連接html
Cdn加速配置後,常常碰到客戶反饋cdn 緩存不能命中,這裏對緩存不能命中進行了總結
問題一:源站設置了no cache,private
(1) Cache control設置nocache, private
swift
curl -I http://m.xiaohaizi.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.hnteacher.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設置cookie
問題二:cdn設置對應規則緩存時間爲0
curl -I http://dccdn.pier39.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: 3adad78914750670995691589ecurl
排查:1)源站未設置nocache private等;2)源站存在Cache-Control緩存頭
3)查CDN緩存規則配置:
url
問題定位:cdn配置了緩存時間爲0
解決:取消該cdn緩存規則的配置spa
問題三:cdn 未設置緩存規則,而且源站也未拋出與緩存相關的頭如cache control,Expires,(Last-Modified/If-Modified-Since, Etag/If-None-Match要配合Cache-Control使用)等
.net
解決:cdn設置緩存規則;
建議: 建議設置目錄緩存 / ,權重設置的低一點,來保證全站緩存
參考:https://help.aliyun.com/document_detail/27136.html?spm=5176.doc27140.6.139.ddB4Tw3d
問題四:源站設置:cache-control:max-age=0
orm
解決:取消源站的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