在開發(fā)調(diào)試web的時候,經(jīng)常會碰到因瀏覽器緩存(cache)而經(jīng)常要去清空緩存或者強制刷新來測試的煩惱,提供下apache不緩存配置和nginx不緩存配置的設置。在常用的緩存設置里面有兩種方式,都是使用add_header來設置:分別為Cache-Control和Pragma。
nginx: location ~ .*\.(css|js|swf|php|htm|html )$ { add_header Cache-Control no-store;
add_header Pragma no-cache;
}
以Nginx服務器為例:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { #過期時間為30天, #圖片文件不怎么更新,過期可以設大一點, #如果頻繁更新,則可以設置得小一點。 expires 30d; } location ~ .*\.(js|css)$ { expires 10d; }
【背景】:Expires是Web服務器響應消息頭字段,在響應http請求時告訴瀏覽器在過期時間前瀏覽器可以直接從瀏覽器緩存取數(shù)據(jù),而無需再次請求。
【相關資料】
1、Cache-control策略
Cache-Control與Expires的作用一致,都是指明當前資源的有效期,控制瀏覽器是否直接從瀏覽器緩存取數(shù)據(jù)還是重新發(fā)請求到服務器取數(shù)據(jù)。只不過Cache-Control的選擇更多,設置更細致,如果同時設置的話,其優(yōu)先級高于Expires。
http協(xié)議頭Cache-Control :
值可以是public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age
各個消息中的指令含義如下:
Last-Modified/If-Modified-Since
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/>
<meta http-equiv="expires" content="0"/>
更多建議: