三种 .htaccess 过期规则之间的区别

发布于 2024-09-24 03:11:09 字数 220 浏览 2 评论 0原文

以下三个 .htaccess 规则之间有什么区别以及何时使用每个规则,是否有任何特殊用例更喜欢其中一个?:

Header set Cache-Control "max-age=290304000"
Header set Expires "Thu, 15 Apr 2020 20:00:00 GMT"
ExpiresDefault "access plus 10 years"

What's the difference between the following three .htaccess rules and when to use each, is there any special use cases that prefers one over the other?:

Header set Cache-Control "max-age=290304000"
Header set Expires "Thu, 15 Apr 2020 20:00:00 GMT"
ExpiresDefault "access plus 10 years"

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

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

发布评论

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

评论(2

迟月 2024-10-01 03:11:09

Header 是一个指令mod_headers 允许修改 HTTP 标头字段。在这种情况下,标头设置有效地设置提到的标头字段Cache-Control过期,因此已经存在的标头字段将被覆盖。

第一个指令将标头字段 Cache-Control 设置为值 max-age=290304000,该值将新鲜度生命周期描述为相对于响应时间的 290304000 秒。

与此相反,第二个指令将标头字段 Expires 设置为值 Thu, 15 Apr 2020 20:00:00 GMT,该值描述了新鲜度生命周期的绝对时间价值。

两者Cache-Control max-age 值和 Expires 过期时间戳可以相互转换

4.2.1。计算保鲜期

缓存可以计算新鲜度寿命(表示为
freshness_lifetime)通过使用第一个匹配的响应
以下:

  • ...

  • 如果 max-age 响应指令(第 5.2 节.2.8) 存在,
    使用它的值,或者

  • 如果响应标头字段过期(第 5.3 节) 存在,使用
    其值减去 Date 响应标头字段的值,或者

  • ...

但是如果两者都存在,Cache-Controlmax-age 优于 Expires

如果响应包含带有 max-age 的 Cache-Control 字段
指令(第 5.2.2.8 节),接收者必须忽略过期的
场地。同样,如果响应包含 s-maxage 指令
(第 5.2.2.9 节),共享缓存接收者必须忽略过期时间
场地。在这两种情况下,Expires 中的值仅用于
适用于尚未实现 Cache-Control 字段的接收者。

无需手动设置这些 HTTP 缓存控制标头字段, mod_expires ExpiresDefault 指令 允许轻松设置 HTTP 缓存。新鲜度生命周期可以用绝对值或相对值来描述,可以相对于响应时间(即access/now),也可以相对于修改时间请求的文件(即修改)。它同时使用Cache-ControlExpires

在这种情况下,第三个指令将默认的新鲜度设置为从响应时间起 10 年。

我会使用 mod_expires 进行 HTTP 缓存控制,而不是使用 Header 手动执行。它更加方便,允许相对和绝对新鲜时间,并使用Cache-ControlExpires

Header is a directive of mod_headers that allows to modify HTTP header fields. In this case Header set effectively sets the mentioned header fields Cache-Control and Expires, so an already existing header field will be overwritten.

The first directive sets the header field Cache-Control with the value max-age=290304000, that describes the freshness lifetime to be 290304000 seconds relative to the response time.

In contrast to that, the second directive sets the header field Expires with the value Thu, 15 Apr 2020 20:00:00 GMT that describes the freshness lifetime with an absolute time value.

Both Cache-Control’s max-age value and Expires expiration time stamp can be transformed to the other:

4.2.1. Calculating Freshness Lifetime

A cache can calculate the freshness lifetime (denoted as
freshness_lifetime) of a response by using the first match of the
following:

  • ...

  • If the max-age response directive (Section 5.2.2.8) is present,
    use its value, or

  • If the Expires response header field (Section 5.3) is present, use
    its value minus the value of the Date response header field, or

  • ...

But if both are present, Cache-Control’s max-age is preferred over Expires:

If a response includes a Cache-Control field with the max-age
directive (Section 5.2.2.8), a recipient MUST ignore the Expires
field. Likewise, if a response includes the s-maxage directive
(Section 5.2.2.9), a shared cache recipient MUST ignore the Expires
field. In both these cases, the value in Expires is only intended
for recipients that have not yet implemented the Cache-Control field.

Instead of setting these HTTP caching control header fields manually, mod_expires ExpiresDefault directive allows an easy setup for HTTP caching. The freshness lifetime can either be described with an absolute value or with a relative value, either relative to the response time (i.e. access/now) or relative to the modification time of the requested file (i.e. modification). It uses both Cache-Control and Expires.

In this case the third directive sets the default freshness lifetime to be 10 years from the time of response on.

I would use mod_expires for HTTP cache control instead of doing it manually with Header. It is far more convenient, allows both relative and absolute freshness times and uses both Cache-Control and Expires.

往事风中埋 2024-10-01 03:11:09

第一条规则向 Cache-Control 添加 max-age 条目。浏览器必须在此处给出的时间(以秒为单位)之后重新获取文档。

第二条和第三条规则创建过期标头。浏览器必须在此处给出的日期重新获取文档。服务器必须做一些计算。

请注意,第二条规则强制同时刷新所有浏览器和所有资源,而第三条也是最后一条规则则根据请求时间使浏览器缓存失效。如果您的网站流量很大,您会看到差异:您可能会在 2020 年 4 月 15 日达到戏剧性的峰值。:)

根据经验:使用 max-age。 这是非常< a href="http://www.mnot.net/blog/2007/05/15/expires_max-age" rel="nofollow noreferrer">良好支持 并且您没有机会创建无效日期。此外,它更短。

进一步阅读:

10 个网址使用“01 jan 0001”的到期日期权威地声明它们已过期(并且可能已木乃伊化)。

The first rule adds a max-age entry to Cache-Control. The browser has to refetch the document after the time in seconds given here.

The second and the third rules create expires headers. The browser has to refetch the document at the date given here. And the server has to do some calculations.

Note that the second rule forces a refresh for all browsers and all resources at the same time, while the third and the last rule invalidate the browser cache depending on the request time. You will see the difference if you have a site with much traffic: You will probably get a dramatic peak at 15 Apr 2020. :)

As a rule of thumb: Use max-age. It is very good supported and you don’t have the chance to create an invalid date. Additionally, it’s shorter.

Further reading:

10 URLs authoritatively stated they are expired (and probably mummified) by using an expiry of "01 jan 0001".

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