设置网站相关文件缓存到本地之目的,就是让网页里的图片、js、css等不经常变化的文件资源缓存到本地,而不是每次都去服务器上重复下载,这样做可以减轻服务器的带宽压力并使网页打开速度加快,这样一来用户访问网站的体验就会好一些。 那么如何来...
设置网站相关文件缓存到本地之目的,就是让网页里的图片、js、css等不经常变化的文件资源缓存到本地,而不是每次都去服务器上重复下载,这样做可以减轻服务器的带宽压力并使网页打开速度加快,这样一来用户访问网站的体验就会好一些。
那么如何来设置相关文件缓存呢?下面拟介绍两种方法(mod_expires.so或mod_headers.so均可实现缓存文件功能,但不宜同时开启两种缓存方式,选择其一就可):
方法之一, 找到并打开d:\apache\conf\下的httpd.conf文件:
(一)查找去除下面代码前面的#号,开启缓存模块mod_expires.so
LoadModule expires_module modules/mod_expires.so
(二)添加设置缓存规则
#缓存代码 <IfModule mod_expires.c> ExpiresActive On # 文件缓存2592000/3600/24=30天 ExpiresByType image/x-icon A2592000 ExpiresByType application/x-javascript A2592000 ExpiresByType text/css A2592000 # 配合前面不压缩gif、png、jpeg等格式图片而进行缓存以提升访问速度。 ExpiresByType image/gif A604800 # 文件缓存604800/3600/24=7天 ExpiresByType image/png A604800 ExpiresByType image/jpeg A604800 ExpiresByType text/plain A604800 ExpiresByType application/x-shockwave-flash A604800 ExpiresByType video/x-flv A604800 ExpiresByType application/pdf A604800 # 文章页、栏目页、及网站首页等经常会更新,且文章页需要更新统计数据,故不宜缓存。 ExpiresByType text/html A0 # 缺省默认缓存86400/3600/24=1天 ExpiresDefault A86400 </IfModule>
方法之二,找到并打开d:\apache\conf\下的httpd.conf文件,查找:
(一)查找去除下面代码前面的#号,开启缓存模块mod_headers.so
LoadModule headers_module modules/mod_headers.so
(二)添加设置缓存规则
#缓存代码 <IfModule mod_headers.c> <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> <FilesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=18000, public, must-revalidate" </FilesMatch> <FilesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=3600, must-revalidate" </FilesMatch> </IfModule>