使用 Apache 为文件系统中不存在的路径添加过期标头
为了使 CDN 失效,我需要向站点 URL 的路径元素添加前缀。每当发布新版本的资产时,这一点都会发生变化。
然后使用 mod_rewrite 重写 URL: http://example.com/cdn/20111030/ images/image.jpg 到 http://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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来,如果您在 Apache 配置中为您自己的解决方案添加 RewriteEngine/Rule,则位置会被正确拾取,并为 /cdn 调用提供 Expires/Cache-Control 服务,并且不会为非 cdn 调用提供服务,一个小改动:
我看不出这应该是 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:
I can't see a reason this should be a problem in the Apache config.
来自http://drupal.org/node/974350#comment-5305368
这些规则适用于 480 周,但您可以相应调整时间。
另请参阅 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.
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.
解决方案可能是将 Expires 应用于所有资产,使用 mod_headers 从非 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.:
It's a bit overkill for the root of the website, but if only applied to the assets, would be less of an issue.