百度云观测优化建议解决方案:未设置max-age或expires
網(wǎng)頁的緩存是由 HTTP 消息頭中的 “Cache-control” 來控制的,常見的取值有 private、no-cache、max-age、must-revalidate 等,默認(rèn)為private。其作用根據(jù)不同的重新瀏覽方式分為以下幾種情況:
(1) 打開新窗口
如果指定cache-control的值為private、no-cache、must-revalidate,那么打開新窗口訪問時都會重新訪問服務(wù)器。而如果指定了max-age值,那么在此值內(nèi)的時間里就不會重新訪問服務(wù)器,例如:
Cache-control: max-age=5表示當(dāng)訪問此網(wǎng)頁后的5秒內(nèi)再次訪問不會去服務(wù)器。
?
(2) 在地址欄回車
如果值為private或must-revalidate(和網(wǎng)上說的不一樣),則只有第一次訪問時會訪問服務(wù)器,以后就不再訪問。如果值為no-cache,那么每次都會訪問。如果值為max-age,則在過期之前不會重復(fù)訪問。
(3) 按后退按扭
如果值為private、must-revalidate、max-age,則不會重訪問,而如果為no-cache,則每次都重復(fù)訪問
(4) 按刷新按扭
無論為何值,都會重復(fù)訪問
當(dāng)指定Cache-control值為“no-cache”時,訪問此頁面不會在Internet臨時文章夾留下頁面?zhèn)浞荨?/p>
另外,通過指定“Expires”值也會影響到緩存。例如,指定Expires值為一個早已過去的時間,那么訪問此網(wǎng)時若重復(fù)在地址欄按回車,那么每次都會重復(fù)訪問:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
在ASP中,可以通過Response對象的Expires、ExpiresAbsolute屬性控制Expires值;通過Response對象的CacheControl屬性控制Cache-control的值,例如:
Response.ExpiresAbsolute = #2000-1-1# ' 指定絕對的過期時間,這個時間用的是服務(wù)器當(dāng)?shù)貢r間,會被自動轉(zhuǎn)換為GMT時間Response.Expires = 20 ' 指定相對的過期時間,以分鐘為單位,表示從當(dāng)前時間起過多少分鐘過期。Response.CacheControl = "no-cache"Expires值是可以通過在Internet臨時文件夾中查看臨時文件的屬性看到的。
?
Apache 主機(jī)解決方案:
Apache 設(shè)置緩存可以通過 mod_headers 模塊修改 cache-control 來實(shí)現(xiàn)。
header set cache-control "max-age="3600"。mod_expires 實(shí)例:
<Directory /opt>ExpiresActive OnExpiresDefault "accesss plus 3600 seconds" 如果是1秒,后面也是secondsExpiresByType application/octet-stream "accesss plus 1 months" 這是對特殊文件類型bin緩存1個月<FilesMatch ^data.swf$> 針對opt目錄下data.swf設(shè)置Expire值ExpiresActive OnExpiresDefault "accesss plus 60 seconds"</FilesMatch></Directory>mod_headers 實(shí)例:
<Directory /opt>header set cache-control "max-age=3600"<FilesMatch ^data.swf$>header set cache-control "max-age=60"</FilesMatch></Directory><FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">Header set Cache-Control "max-age=2592000"</FilesMatch>?
Nginx 主機(jī)解決方案:
Nginx 的 ngx_http_headers_module 模塊可以對 Cache-Control 頭相關(guān)的東西進(jìn)行配置,例如:
if ($request_uri ~* "^/$|^/search/.+/|^/company/.+/") {add_header Cache-Control max-age=3600;}if ($request_uri ~* "^/search-suggest/|^/categories/") {add_header Cache-Control max-age=86400;}expires 的設(shè)置方法,增加至主機(jī) conf 文件中:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~* \.(ico|gif|jpg|jpeg|png|bmp|swf)(\?[0-9]+)?${expires max;break;}?
通過 HTTP 的 META 設(shè)置 expires 和 cache-control 解決方案:
<meta http-equiv="Cache-Control" content="max-age=7200" /><meta http-equiv="Expires" content="Mon, 18 Aug 2014 23:00:00 GMT" />expires 用于設(shè)定網(wǎng)頁的過期時間,也可設(shè)置為:
<meta http-equiv="Cache-Control" CONTENT="no-cache"><meta http-equiv="Expires" content="0" />?
CSS 中圖片、字體及 JS 的 max_age 解決方案:
background-image:url(sprite/bg.png?max_age=31536000&d=05231747)格式為?d=月日小時分鐘,其他文件如此類推。
原文鏈接:http://www.imjeff.cn/blog/223/
轉(zhuǎn)載于:https://www.cnblogs.com/xiamidong/p/4535362.html
總結(jié)
以上是生活随笔為你收集整理的百度云观测优化建议解决方案:未设置max-age或expires的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)Spring中Bean的命名问题(
- 下一篇: node-webkit File Di