Header 和 Expries 是用来控制浏览器的缓存控制模块,通过开启该模块并进行一定的设置可以有效加速网站减少请求数和数据流量。
一般我们可以通过设置 HTTP 响应的 Head 中的 expired 和 cache-control 来控制页面在浏览器中的缓存 例如: Cache-Control: max-age=1800 Expires: Wed, 18 May 2012 21:00:00 GMT 缓存最大时长为1800秒,设定的过期时间为2012-05-18 21:00:00 Apache开启这两个模块非常简单(Debian环境) a2enmod headers a2enmod expries 开启后重启下apache即可。 如何充分利用这两个模块,关键在于配置。该配置可以配置到.htaccess文件下也可以在apache的网页控制文件下加入 我是直接在配置文件 /etc/apache2/sites-enabled/000-default文件里配置的
# ---------------------------------------------------------------------- # Expires headers (for better cache control) # ---------------------------------------------------------------------- # these are pretty far-future expires headers # they assume you control versioning with cachebusting query params like # <script src="application.js?20100608"> # additionally, consider that outdated proxies may miscache # www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/ # if you don't use filenames to version, lower the css and js to something like # "access plus 1 week" or so <IfModule mod_expires.c> ExpiresActive on # Perhaps better to whitelist expires rules? Perhaps. ExpiresDefault "access plus 1 month" # cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5) ExpiresByType text/cache-manifest "access plus 0 seconds" # your document html ExpiresByType text/html "access plus 0 seconds" # data ExpiresByType text/xml "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType application/json "access plus 0 seconds" # rss feed ExpiresByType application/rss+xml "access plus 1 hour" # favicon (cannot be renamed) ExpiresByType image/x-icon "access plus 1 week" # media: images, video, audio ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType video/ogg "access plus 1 month" ExpiresByType audio/ogg "access plus 1 month" ExpiresByType video/mp4 "access plus 1 month" ExpiresByType video/webm "access plus 1 month" # htc files (css3pie) ExpiresByType text/x-component "access plus 1 month" # webfonts ExpiresByType font/truetype "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType application/x-font-woff "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" # css and javascript ExpiresByType text/css "access plus 1 year" ExpiresByType application/javascript "access plus 1 year" ExpiresByType text/javascript "access plus 1 year" <IfModule mod_headers.c> Header append Cache-Control "public" </IfModule> </IfModule> 这是.htaccess下的一段配置代码,如果是在000-default文件下配置,则直接将中间的那段代码加入到 文件开头
<VirtualHost *:443> /*
ExpiresActive on
ExpiresDefault
*/ ServerAdmin webmaster@localhost ….. 关于expires的配置说明: ExpiresActive 指令:打开或关闭产生”Expires:”和”Cache-Control:”头的功能。 ExpiresByType 指令:指定MIME类型的文档(例如:text/html)的过期时间。 ExpiresDefault 指令:默认所有文档的过期时间。 过期时间的写法: “access plus 1 month” “access plus 4 weeks” “now plus 30 days” “modification plus 5 hours 3 minutes” A2592000 M604800 access、now及A 三种写法的意义相同,指过期时间从访问时开始计算。 modification及M 的意义相同,指过期时间是以被访问文件的最后修改时间开始计算。 所以,后一种写法只对静态文件起作用,而由脚本生成的动态页面不受它的作用。