出于CDN失效的目的,我需要在网站的URL的路径元素中添加前缀。每当发布新版本的资产时,都会更改此信息。
然后使用mod_rewrite将URL重写为:http://example.com/cdn/20111030/images/image.jpg到http://example.com/images/image.jpg,这是资产实际所在的位置。
我想在响应中添加长期到期标题(至少3个月)(对于文件系统中实际不存在的第一个URL)。有谁知道怎么做?
答案 0 :(得分:5)
如果您在Apache配置中为自己的解决方案添加RewriteEngine / Rule,则可以正确选择Location并在/ 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配置中看到这个问题。
答案 1 :(得分:4)
来自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 rules,因为这些处理服务器没有安装mod_headers或mod_expires。它使用FilesMatch指令; advagg文件有一个相当独特的文件名,因此我可以这样做。 AdvAgg后备在这种情况下不起作用,因为mod_expires不能使用环境变量; FileETag也不能。从我所看到的情况来看,mod_headers是在apache中设定远期未来时间的理想方式。
答案 2 :(得分:0)
解决方案可以是将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
对于网站的根源来说,这有点过分,但如果仅应用于资产,则不会成为问题。