使用 mod_expires 覆盖缓存标头

发布于 2024-12-12 18:18:31 字数 867 浏览 0 评论 0原文

我想使用 apache 的 mod_expires 模块设置缓存头。我的配置看起来有点像这样:

<LocationMatch ^/static >
    ExpiresDefault "access plus 1 years"
</LocationMatch>

问题是,这些文件是由我无法控制的第三个系统生成的。这些系统提供具有以下标头的文件:

Date Mon, 24 Oct 2011 08:39:02 GMT
Cache-Control no-cache,no-store,must-revalidate
Pragma no-cache
Expires Thu, 01 Dec 1994 16:00:00 GMT

这些标头使得无法使用 mod_expires 设置缓存标头。 http://httpd.apache.org/docs/2.2/mod/mod_expires.html 告诉我们原因:

当 Expires 标头已经是服务器生成的响应的一部分时,例如由 CGI 脚本生成或从源服务器代理时,此模块不会更改或添加 Expires 或 Cache-Control 标头。

有没有可能的方法来规避此规则并用 mod_expires 覆盖标头?

更新: 为了避免此限制,一种可能的解决方案是仅使用 mod_headers 来设置缓存头。不幸的是,这不是一个替代方案,因为必须计算这些值。

谢谢它提前。

I want to set cache-headers using the mod_expires module from apache. My configuration looks somewhat like this:

<LocationMatch ^/static >
    ExpiresDefault "access plus 1 years"
</LocationMatch>

The problem is, that the files are generated by a third system I don't control. These system provides files with the following headers:

Date Mon, 24 Oct 2011 08:39:02 GMT
Cache-Control no-cache,no-store,must-revalidate
Pragma no-cache
Expires Thu, 01 Dec 1994 16:00:00 GMT

These headers makes it impossible to set the cache-headers with mod_expires. http://httpd.apache.org/docs/2.2/mod/mod_expires.html tells us why:

When the Expires header is already part of the response generated by the server, for example when generated by a CGI script or proxied from an origin server, this module does not change or add an Expires or Cache-Control header.

Is there any possible way to circumvent this rule and overwrite the headers with mod_expires?

Update:
One possible solution, to avoid this limitation is to use only mod_headers to set the cache-headers. Unfortunately, this isn't an alternative because the values have to be calculated.

Thanks it advance.

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

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

发布评论

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

评论(3

梦巷 2024-12-19 18:18:31

不幸的是,这是一个已知的限制,我们不得不只使用 mod_headers

Unfortunately, it's a known limitation and we had to fall back to use only mod_headers.

轮廓§ 2024-12-19 18:18:31

Regilero 的建议行不通,因为标头指令将在响应处理的最后阶段(在 mod_expire 指令之后)进行处理。因此,您可以在 mod_expires 执行(或未执行)其应执行的操作后取消设置标头。

如果是 apache 2.2,您可以尝试将 early 放在每个标头指令的末尾。这将告诉它在响应处理的早期阶段而不是最后阶段执行此操作。

所以尝试一下:

<LocationMatch ^/static >
  Header unset Cache-Control early
  Header unset Pragma early
  Header unset Expires early
  ExpiresDefault "access plus 1 years"
</LocationMatch>

虽然还没有测试过,但尝试一下......

Regilero's suggestion won't work because header directives will be processed very late in the response processing - after mod_expire directive. So you'd unset the headers after mod_expires did (or didn't) what it was supposed to do.

If it's apache 2.2 you could try putting early at the end of each header directive. That will tell it to do this in an early stage of response processing as opposed to at the end.

so try:

<LocationMatch ^/static >
  Header unset Cache-Control early
  Header unset Pragma early
  Header unset Expires early
  ExpiresDefault "access plus 1 years"
</LocationMatch>

Haven't tested tho, but give it a try...

多彩岁月 2024-12-19 18:18:31

您是否尝试过将其与 mod_headers 混合?

<LocationMatch ^/static >
  Header unset Cache-Control 
  Header unset Pragma
  Header unset Expires 
  ExpiresDefault "access plus 1 years"
</LocationMatch>

未经测试,但如果...

Have you tried mixing it with mod_headers?

<LocationMatch ^/static >
  Header unset Cache-Control 
  Header unset Pragma
  Header unset Expires 
  ExpiresDefault "access plus 1 years"
</LocationMatch>

Not tested, but in case of...

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