这里我使用的是Apache2.4.17javascript
打开apache安装目录,找到conf目录,用记事本打开httpd.conf 文件。css
ctrl+f 查找 LoadModule expires_module modules/mod_expires.so
去掉前面 #号!html
在文本最后面添加:java
<IfModule expires_module> #打开缓存 ExpiresActive on #css文件缓存7200000/3600/24=83天 ExpiresByType text/css A7200000 #js文件缓存83天 ExpiresByType application/x-javascript A7200000 ExpiresByType application/javascript A7200000 #html文件缓存83天 ExpiresByType text/html A7200000 #图片文件缓存83天 ExpiresByType image/jpeg A7200000 ExpiresByType image/gif A7200000 ExpiresByType image/png A7200000 ExpiresByType image/x-icon A7200000 </IfModule>
上面开启的是expireapache
下面是cache-control
在文本后面继续添加浏览器
<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> <FilesMatch "\.(css|js)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch>
这里时间设置不同是为了检验是否成功配置的,由于,没设cache-control的时候,它会自动根据expire的时间设置本身。缓存
最后重启apache服务器,ok!服务器
这里说一个本身爬的坑吧!(那就是若是不出现 200 form cache, 而是出现了304,那是由于,你刷新了浏览器。。。想要出现 200 form cache,须要在浏览器地址栏里按回车键。)app
也就是说:刷新浏览器 触发 304, 地址栏回车触发 200 form cache。code
查了很久才发现了,以前一直想不通,既然设置了cache-control和expire为啥仍是304,这就是缘由,就是这么简单。。。