我有一个服务器,它根据Accept报头返回不同的响应,例如,如果Accept报头包含" image / webp ",则提供webp图像,否则提供jpg。
我们在服务器级别运行Varnish,它可以正确地执行此操作,如下面的示例所示:
请求( Accept header中包含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我们在规则引擎中设置了以下选项,但是,无论请求标头如何,先缓存的内容类型将服务于所有后续请求。
规则引擎设置

有谁知道实现这一点的方法吗?
提前感谢!
发布于 2019-08-15 14:04:45
我们用Verizon/Edgecast:也遇到了同样的问题,一个网址根据Accept报头提供了两种不同的图像类型(JPEG和WebP)。源(imgix)发送了正确的Vary: Accept,但Edgecast忽略了这一点,并缓存了它获得的内容,因此没有WebP支持的浏览器有时会得到错误的格式。
我们用Edgecast中的一条规则解决了这个问题:WebP rule
查询参数auto始终是URL的一部分,因此可以始终从缓存键中删除。使用第二个查询参数varyWebP,我们可以明确地识别URL,并防止与没有查询参数auto的URL发生冲突。
在本例中,URL
https://[HOST]/[PATH]?a=1&b=2&c=3&auto=compress,format创建与以下内容相同的缓存键:
https://[HOST]/[PATH]?a=1&b=2&c=3这就是查询参数varyWebP保护我们的原因。
https://stackoverflow.com/questions/54562677
复制相似问题