4

我想利用浏览器缓存来提高页面速度。听起来 max-age 和 last-modified 是不错的选择,但我不清楚如何确定应该为它实现哪些文件。一般来说,我对如何实际执行此操作以及代码在我的 htaccess 中的外观感到困惑。我想我希望获得一些更明确的帮助或展示一些示例。或者也许有人可以指导我上一个像我这样的新手可以理解的课程/教程,但我没有找到任何运气。任何了解 max-age 和 last-modified 并且可以帮助告诉我如何做到这一点的人的任何帮助将不胜感激。我真的很迷茫,会花钱请人来帮助我。谢谢。

4

1 回答 1

9

在这里搜索 SO 会返回一些很好的信息——比如利用浏览器缓存——但无论如何......

来自: http: //www.samaxes.com/2011/05/improving-web-performance-with-apache-and-htaccess/

首次访问您的页面的访问者会发出多个 HTTP 请求来下载所有站点文件,但通过使用ExpiresCache-Control标头,您可以使这些文件可缓存。这避免了对后续页面视图的不必要的 HTTP 请求。

由于mod_expiresmod_headers模块,Apache 启用了这些标头。

mod_expires模块控制 HTTP 标头的设置和服务器响应中 HTTP 标Expires头的max-age指令。Cache-Control

要修改Cache-Control以外的指令max-age,您可以使用该mod_headers模块。

mod_headers模块提供指令来控制和修改 HTTP 请求和响应标头。标题可以合并、替换或删除。

设置Expires标题的规则:

# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers

设置Cache-Control标题的规则:

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers

注意:不需要max-age使用 header 设置指令,Cache-Control因为它已经由mod_expires模块设置。

must-revalidate意味着一旦响应变得陈旧,就必须重新验证;这并不意味着每次都必须检查它。

更多信息在这里:http
://www.mnot.net/cache_docs/ 来自谷歌:http
://code.google.com/speed/page-speed/docs/caching.html 和雅虎:http://developer。 yahoo.com/performance/rules.html#expires

于 2011-07-29T22:34:09.133 回答