使用 Apache 为文件系统中不存在的路径添加过期标头

发布于 2024-12-12 14:09:45 字数 405 浏览 0 评论 0原文

为了使 CDN 失效,我需要向站点 URL 的路径元素添加前缀。每当发布新版本的资产时,这一点都会发生变化。

然后使用 mod_rewrite 重写 URL: http://example.com/cdn/20111030/ images/image.jpghttp://example.com/images/image.jpg 这是资产实际所在的位置。

我想在响应中添加长过期标头(至少 3 个月)(对于文件系统中实际不存在的第一个 URL)。有谁知道该怎么做?

For the purposes of CDN invalidation I need to add a prefix to the path element of a site's URL. This is changed whenever a new version of the asset is released.

The URL is then rewritten using mod_rewrite from: http://example.com/cdn/20111030/images/image.jpg to http://example.com/images/image.jpg which is where the asset actually resides.

I would like to add long expiry headers (at least 3 months) to the response (for the first URL which doesn't actually exist in the filesystem). Does anyone know how to do this?

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

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

发布评论

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

评论(3

谎言月老 2024-12-19 14:09:46

看来,如果您在 Apache 配置中为您自己的解决方案添加 RewriteEngine/Rule,则位置会被正确拾取,并为 /cdn 调用提供 Expires/Cache-Control 服务,并且不会为非 cdn 调用提供服务,一个小改动:

    # in apache config
    RewriteEngine On
    RewriteRule ^/cdn/[^/]*/(.*) /$1 [L]

    <Location "/cdn">
      Header unset ETag
      FileETag None
      ExpiresActive on
      ExpiresDefault "access plus 1 year"
    </Location>

我看不出这应该是 Apache 配置中的问题的原因。

It appears that if you add the RewriteEngine/Rule in the Apache configuration for your own solution, the Location is picked up correctly and serves the Expires/Cache-Control on /cdn calls and doesn't serve them for non-cdn calls, with a minor changee:

    # in apache config
    RewriteEngine On
    RewriteRule ^/cdn/[^/]*/(.*) /$1 [L]

    <Location "/cdn">
      Header unset ETag
      FileETag None
      ExpiresActive on
      ExpiresDefault "access plus 1 year"
    </Location>

I can't see a reason this should be a problem in the Apache config.

眼眸里的那抹悲凉 2024-12-19 14:09:46

来自http://drupal.org/node/974350#comment-5305368
这些规则适用于 480 周,但您可以相应调整时间。

<IfModule mod_rewrite.c>
  RewriteEngine on
  <IfModule mod_headers.c>
    # Transform /cdn/***/ to /
    RewriteCond %{REQUEST_URI} ^/cdn/([0-9a-zA-Z])*/(.+)$
    RewriteRule .* /%2 [L,E=CDN:1]
    # Apache will change CDN to REDIRECT_CDN.

    # Set a far future Cache-Control header (480 weeks), which prevents
    # intermediate caches from transforming the data and allows any
    # intermediate cache to cache it, since it's marked as a public resource.
    Header set Cache-Control "max-age=290304000, no-transform, public" env=REDIRECT_CDN

    # Set a far future Expires header. The maximum UNIX timestamp is somewhere
    # in 2038. Set it to a date in 2037, just to be safe.
    Header set Expires "Tue, 20 Jan 2037 04:20:42 GMT" env=REDIRECT_CDN

    # Pretend the file was last modified a long time ago in the past, this will
    # prevent browsers that don't support Cache-Control nor Expires headers to
    # still request a new version too soon (these browsers calculate a
    # heuristic to determine when to request a new version, based on the last
    # time the resource has been modified).
    # Also see http://code.google.com/speed/page-speed/docs/caching.html.
    Header set Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT" env=REDIRECT_CDN

    # Do not use etags for cache validation.
    Header unset ETag env=REDIRECT_CDN
  </IfModule>
</IfModule>

另请参阅 AdvAgg规则,因为这些处理未安装 mod_headers 或 mod_expires 的服务器。它使用 FilesMatch 指令; advagg 文件有一个相当独特的文件名,因此我可以这样做。 AdvAgg 后备在这种情况下不起作用,因为 mod_expires 无法使用环境变量变量; FileETag 也不能。据我所见, mod_headers 是设置所需的方式阿帕奇的遥远未来时代。

From http://drupal.org/node/974350#comment-5305368
These rules are for 480 weeks but you can adjust the time accordingly.

<IfModule mod_rewrite.c>
  RewriteEngine on
  <IfModule mod_headers.c>
    # Transform /cdn/***/ to /
    RewriteCond %{REQUEST_URI} ^/cdn/([0-9a-zA-Z])*/(.+)$
    RewriteRule .* /%2 [L,E=CDN:1]
    # Apache will change CDN to REDIRECT_CDN.

    # Set a far future Cache-Control header (480 weeks), which prevents
    # intermediate caches from transforming the data and allows any
    # intermediate cache to cache it, since it's marked as a public resource.
    Header set Cache-Control "max-age=290304000, no-transform, public" env=REDIRECT_CDN

    # Set a far future Expires header. The maximum UNIX timestamp is somewhere
    # in 2038. Set it to a date in 2037, just to be safe.
    Header set Expires "Tue, 20 Jan 2037 04:20:42 GMT" env=REDIRECT_CDN

    # Pretend the file was last modified a long time ago in the past, this will
    # prevent browsers that don't support Cache-Control nor Expires headers to
    # still request a new version too soon (these browsers calculate a
    # heuristic to determine when to request a new version, based on the last
    # time the resource has been modified).
    # Also see http://code.google.com/speed/page-speed/docs/caching.html.
    Header set Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT" env=REDIRECT_CDN

    # Do not use etags for cache validation.
    Header unset ETag env=REDIRECT_CDN
  </IfModule>
</IfModule>

Also see the AdvAgg rules as these handle servers that do not have mod_headers or mod_expires installed. It uses a FilesMatch directive; advagg files have a fairly unique filename, thus I can do this. The AdvAgg fallbacks wont work in this case because mod_expires can't use environmental variables; neither can FileETag. From what I can see, mod_headers is the desired way to set far future times in apache.

三月梨花 2024-12-19 14:09:46

解决方案可能是将 Expires 应用于所有资产,使用 mod_headers 从非 cdn 版本中删除标头,例如:

 RewriteEngine on
 RewriteRule ^cdn/([0-9a-z])*/(.*) /$2 [L,E=cdn:1]

 ExpiresActive on
 ExpiresDefault "access plus 1 year"
 Header unset Expires env=!cdn
 Header unset Cache-Control env=!cdn

这对于网站的根来说有点大材小用,但如果仅应用于资产,效果会更好的一个问题。

A solution could be to apply the Expires to all assets, the use mod_headers to remove the headers from the non-cdn version, e.g.:

 RewriteEngine on
 RewriteRule ^cdn/([0-9a-z])*/(.*) /$2 [L,E=cdn:1]

 ExpiresActive on
 ExpiresDefault "access plus 1 year"
 Header unset Expires env=!cdn
 Header unset Cache-Control env=!cdn

It's a bit overkill for the root of the website, but if only applied to the assets, would be less of an issue.

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