利用浏览器缓存

发布于 2024-11-26 18:54:59 字数 260 浏览 2 评论 0原文

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

I want to leverage browser caching to increase page speed. It sounds like max-age and and last-modified are good choices, but I'm unclear on how to determine which files I should implement for it. In general, I'm confused on how to actually do this and what the code would look like in my htaccess. I guess I'm looking to get some more explicit help or to be shown some examples. Or maybe someone can direct me to a lesson/tutorial on this that a novice like me can understand, which I haven't had any luck finding. Any help from someone who knows more about max-age and last-modified and can help tell me how to do this would be greatly appreciated. I am really lost on this and would pay someone to help me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-12-03 18:54:59

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

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

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

Apache 通过 mod_expiresmod_headers 模块启用这些标头。

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

要修改 max-age 以外的 Cache-Control 指令,您可以使用 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 指令>Cache-Control 标头,因为它已由 mod_expires 模块设置。

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

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

A search here on SO would have returned some good information - like Leverage browser caching - but anyway...

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

A first-time visitor to your page will make several HTTP requests to download all your sites files, but by using the Expires and Cache-Control headers you make those files cacheable. This avoids unnecessary HTTP requests on subsequent page views.

Apache enables those headers thanks to mod_expires and mod_headers modules.

The mod_expires module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses.

To modify Cache-Control directives other than max-age, you can use the mod_headers module.

The mod_headers module provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed.

Rule for setting Expires headers:

# 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

Rule for setting Cache-Control headers:

# 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

Note: There is no need to set max-age directive with Cache-Control header since it is already set by mod_expires module.

must-revalidate means that once a response becomes stale it has to be revalidated; it doesn’t mean that it has to be checked every time.

More info here: http://www.mnot.net/cache_docs/
And from Google: http://code.google.com/speed/page-speed/docs/caching.html
And Yahoo: http://developer.yahoo.com/performance/rules.html#expires

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文