首页 > 解决方案 > 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

我们在规则引擎设置中有以下选项,但是无论请求标头如何,首先缓存的内容类型都会在所有后续请求中提供服务。

规则引擎设置

在此处输入图像描述

有谁知道实现这一目标的方法?

提前致谢!

标签: azurecdnedgecastverizon

解决方案


我们遇到了同样的问题,Verizon/Edgecast:一个 URL根据标题提供了两种不同的图像类型(JPEG和)。原点(imgix)发送正确,但忽略了它并缓存了它得到的东西,所以没有支持的浏览器有时会得到错误的格式。WebPAcceptVary: AcceptEdgecastWebP

我们用 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保护我们的原因。


推荐阅读