浏覽器通路網站的圖檔時會把靜态的檔案緩存在本地電腦裡,這樣下次再通路時就不用去遠端下載下傳了.
當你用浏覽器去通路一個網站,網站裡的所有靜态檔案,比如圖檔、css、js。浏覽器會預設把這些緩存在電腦裡,這樣一個臨時檔案,到底要緩存多長時間,這個是在伺服器上定義的,不去定義浏覽器也不會把這些清空,或者浏覽器有自己的一套機制清緩存,電腦管家也會發現一些緩存檔案幫你清理。
緩存檔案放電腦裡的目的就是讓你之後的通路不在去伺服器上下載下傳這些檔案了。
可在浏覽器上測試。多次加載出現304(因為檔案沒有修改過,是以不需要在伺服器上重新加載),不好的就是什麼時候清緩存,什麼時候去在下載下傳
增加配置
<IfModule mod_expires.c>
ExpiresActive on //打開該功能的開關
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
需要expires_module
/usr/local/apache2.4/bin/apachectl -M |grep expire //這個子產品有沒加載(沒有加載要到主配置檔案去掉子產品之前#)
expires_module (shared)
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
Windows測試強制重新整理ctrl+F5。
規定了過期時間86400秒。過期的日期時間是什麼
curl測試,看cache-control: max-age
curl -x192.168.211.149:80 111.com/qq.jpg -I
HTTP/1.1 200 OK
Date: Wed, 06 Dec 2017 05:26:11 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Last-Modified: Fri, 03 Mar 2017 12:53:47 GMT
ETag: "e267-549d309544cc0"
Accept-Ranges: bytes
Content-Length: 57959
Cache-Control: max-age=86400
Expires: Thu, 07 Dec 2017 05:26:11 GMT
Content-Type: image/jpeg
本文轉自 蝦米的春天 51CTO部落格,原文連結:http://blog.51cto.com/lsxme/2052788,如需轉載請自行聯系原作者