apache的模块里面有一个cache_module模块,这个模块可用于缓存一个url的内容,进而提高服务器的性能。参考(http://httpd.apache.org/docs/2.1/caching.html)
配置比较简单,首先确保
LoadModule cache_module modules/mod_cache.so
LoadModule disk_cache_module modules/mod_disk_cache.so
这2行没有被注释掉。
然后添加下面的配置:
<IfModule cache_module>
CacheIgnoreNoLastMod On
CacheEnable disk /
CacheRoot "c:/temp"
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreCacheControl On
</IfModule>
重新启动apache,再次访问你的网站就能看到效果了。
碰到的问题:
- 如果直接访问首页,而不包括具体的地址,比如http://www.example.com/不会启用cache,而改为http://www.example.com/index.php缓存启用。
- 如果返回的页面为200代码,那response header中必须包含 "Etag", "Last-Modified", "Expires", 或包含maxage或 s-maxage的"Cache-Control,否则该页面不会被缓存,可以用CacheIgnoreNoLastMod来跳过这条规则。
- 当request header中包含Cache-Control: no-cache 或者 Pragma: no-cache的时候,默认不会使用缓存里的内容,比如当按F5的时候,request header中会包含一个Cache-Control: maxage=0,此时页面内容是重新生成的,如果设置CacheIgnoreCacheControl为On,则可以忽略该Cache-Control头部。