apache下用expires_module让浏览器缓存静态文件

让浏览器缓存CSS、JS、图片、静态文件等是很重要的事情,这样可以减轻服务器的压力,省的浏览器经常要去服务端下载这些静态文件。下面看看配置方法吧。

1.开启apache扩展模块mod_expires.so,在apache的配置文件中加入下面一行代码。

LoadModule expires_module modules/mod_expires.so

2.配置缓存,在apache配置文件中加入下面一段代码

<IfModule expires_module>

#打开缓存

ExpiresActive on

#css文件缓存7200000/3600/24=83天

ExpiresByType text/css A7200000

#js文件缓存7200000/3600/24=83天

ExpiresByType application/x-javascript A7200000

ExpiresByType application/javascript A7200000

#html文件缓存7200000/3600/24=83天

ExpiresByType text/html A7200000

#图片文件缓存7200000/3600/24=83天

ExpiresByType image/jpeg A7200000

ExpiresByType image/gif A7200000

ExpiresByType image/png A7200000

ExpiresByType image/x-icon A7200000

</IfModule>

3.测试

访问有图片,css,js,html的文件

http://localhost:8080/test/test.html

用firebug可以看到响应头信息包含

ExpiresTue, 13 Sep 2011 15:41:09 GMT
Cache-Controlmax-age=7200000

如何把不配置缓存,上面的头信息就没有,哈哈,试试吧 。