利用浏览器缓存

And*_*ndy 4 pagespeed

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

小智 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标头的设置和ExpiresHTTP标头的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
Run Code Online (Sandbox Code Playgroud)

设置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
Run Code Online (Sandbox Code Playgroud)

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

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

更多信息:http
://www.mnot.net/cache_docs/来自Google:http://code.google.com/speed/page-speed/docs/caching.html
和Yahoo:http:// developer. yahoo.com/performance/rules.html#expires