小智 9
在SO上搜索会返回一些好的信息 - 比如杠杆浏览器缓存 - 但无论如何......
来自:http://www.samaxes.com/2011/05/improving-web-performance-with-apache-and-htaccess/
首次访问您网页的访问者会发出几个HTTP请求来下载您的所有网站文件,但是通过使用
Expires和Cache-Control标头可以使这些文件可以缓存.这避免了后续页面视图上不必要的HTTP请求.
感谢mod_expires和mod_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
| 归档时间: |
|
| 查看次数: |
7468 次 |
| 最近记录: |