azure - CDN - 根据接受标头 (Verizon/EdgeCast Premium) 提供不同的内容类型?
问题描述
我有一个服务器,它根据 Accept 标头返回不同的响应,例如,如果 Accept 标头包含“image/webp”,则提供 webp 图像,否则提供 jpg。
我们在服务器级别运行 Varnish,它可以正确执行此操作,如下例所示:
请求(在 Accept 标头中带有 image/webp):
curl -s -D - -o /dev/null "https://REDACTED/media/tokinoha_bowl-4.jpg?sh=2&fmt=webp,jpg" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
响应(提供 webp 图像):
HTTP/2 200
date: Wed, 06 Feb 2019 08:25:05 GMT
content-type: image/webp
access-control-allow-origin: *
cache-control: public, s-maxage=31536000, max-age=31536000
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
strict-transport-security: max-age=31536000; includeSubDomains
vary: Accept-Encoding, Accept-Encoding,Origin
referrer-policy: strict-origin-when-cross-origin
accept-ranges: bytes
content-length: 60028
请求(Accept 标头中没有 webp,提供 jpg):
curl -s -D - -o /dev/null "https://REDACTED/media/tokinoha_bowl-4.jpg?sh=2&fmt=webp,jpg" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/apng,*/*;q=0.8"
回复:
HTTP/2 200
date: Wed, 06 Feb 2019 08:25:18 GMT
content-type: image/jpeg
access-control-allow-origin: *
cache-control: public, s-maxage=31536000, max-age=31536000
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
strict-transport-security: max-age=31536000; includeSubDomains
vary: Accept-Encoding, Accept-Encoding,Origin
referrer-policy: strict-origin-when-cross-origin
accept-ranges: bytes
content-length: 166991
我们在规则引擎设置中有以下选项,但是无论请求标头如何,首先缓存的内容类型都会在所有后续请求中提供服务。
规则引擎设置
有谁知道实现这一目标的方法?
提前致谢!
解决方案
我们遇到了同样的问题,Verizon/Edgecast:
一个 URL根据标题提供了两种不同的图像类型(JPEG
和)。原点(imgix)发送正确,但忽略了它并缓存了它得到的东西,所以没有支持的浏览器有时会得到错误的格式。WebP
Accept
Vary: Accept
Edgecast
WebP
我们用 Edgecast 中的一条规则解决了这个问题: WebP 规则
查询参数auto
始终是 的一部分,URL
因此始终可以从缓存键中删除。使用第二个查询参数varyWebP
,我们可以明确地识别 URL,并防止与没有查询参数的 URL 发生冲突auto
。
在这种情况下,网址
https://[HOST]/[PATH]?a=1&b=2&c=3&auto=compress,format
创建与以下相同的缓存键:
https://[HOST]/[PATH]?a=1&b=2&c=3
这就是查询参数varyWebP
保护我们的原因。
推荐阅读
- css - Rubymine css代码完成不起作用
- shell - 如何使用脚本删除文件中重复的填充字符?
- spring-boot - 使用spring boot将文件上传到数据库时出现错误
- css - Google Chrome 浏览器中的 CSS - MAC
- python - 如何在python中的csv文件中写入度数符号并读取与熊猫数据框相同的文件?
- google-sheets - 谷歌表格函数
- javascript - 谁不是每个请求都更新班级成员?
- amazon-web-services - AWS Cloudfront 和 Woocommerce 购物车的问题
- python - 在我降级 Python 版本后,Pip 在 Mac 上坏了
- python - 如何让 pd.read_json 显示较大 .json 文件中的所有数据?