简体   繁体   English

nginx Http2 Push 在 Vary 时失败:接受 header 设置

[英]nginx Http2 Push fails when Vary: Accept header set

Basically, http2 push using http2_push_preload doesn't work if you set header Vary: Accept on your response because you are doing content negotiation using the Accept request header.基本上,如果您设置 header 变化:接受您的响应,则使用 http2_push_preload 的 http2 推送不起作用,因为您正在使用接受请求 header 进行内容协商。 I'm using content negotiation to send (http2 push) webp pics instead of jpg to clients that support it.我正在使用内容协商向支持它的客户端发送(http2 推送)webp 图片而不是 jpg。

HTTP/2 Push works for.js, .css files and all in the same call and shows "Push/Other" in Chrome DevTools, but fails for this one unique case (jpg content negotiated to webp), and shows just "Other" (not pushed) in Chrome DevTools. HTTP/2 推送适用于 .js、.css 文件,所有这些文件都在同一个调用中,并在 Chrome DevTools 中显示“推送/其他”,但对于这种独特的情况(jpg 内容与 webp 协商)失败,并且只显示“其他” (未推送)在 Chrome DevTools 中。

Content negotiation for brotli, gzip compressions all work fine and get pushed properly using the Vary: Accept-Encoding and same for languages using the Vary: Accept-Language. brotli 的内容协商,gzip 压缩都可以正常工作,并使用 Vary: Accept-Encoding 正确推送,对于使用 Vary: Accept-Language 的语言也是如此。

Only Vary: Accept fails.只有变化:接受失败。 Please help I'm at the point of giving up.请帮助我正在放弃。

PS: I was going through nginx source https://github.com/nginx/nginx/blob/master/src/http/v2/ngx_http_v2.c . PS:我正在通过 nginx 源https://github.com/nginx/nginx/blob/master/src/http/v2/ngx_http_v2.Z4A8A08F09D37B7379564903840384 Do a Crtl+F and you will find cases for only "Accept-Encoding" and "Accept-Language", nothing for "Accept".做一个 Crtl+F,你会发现只有“Accept-Encoding”和“Accept-Language”的案例,“Accept”没有。 So I think "Accept" case is not yet supported by nginx??所以我认为 nginx 还不支持“接受”案例?

PPS: I'm not overpushing, only using http2 push for the hero image. PPS:我没有过分推,只使用 http2 推送英雄图像。

Edit: Here's bug ticket on nginx site for those who want to track it: https://trac.nginx.org/nginx/ticket/1851 https://trac.nginx.org/nginx/ticket/1817 Edit: Here's bug ticket on nginx site for those who want to track it: https://trac.nginx.org/nginx/ticket/1851 https://trac.nginx.org/nginx/ticket/1817

Edit 2: Nginx team has responded by saying they are not going to support it due to security reasons (you can find the response in the duplicate bug post), which I believe is due to pushing from different origins like CDNs?编辑 2:Nginx 团队回应说,由于安全原因,他们不会支持它(您可以在重复的错误帖子中找到响应),我认为这是由于来自 CDN 等不同来源的推送? Anyway, I need this feature, so the only option left is to:无论如何,我需要这个功能,所以剩下的唯一选择是:

  1. Create a custom patch or package.创建自定义补丁或 package。

  2. Use some other server software that supports it.使用一些其他支持它的服务器软件。

  3. Manually implement in website code a feature to rewrite.jpg paths to.jpg.webp if requests are coming from clients that support webp.如果请求来自支持 webp 的客户端,则在网站代码中手动实现将.jpg 路径重写为.jpg.webp 的功能。

(I don't give up:P) (我不放弃:P)

I'm not entirely surprised by this and Apache does the same .我对此并不完全感到惊讶, Apache 也是如此 If you want this to change suggest to raise a bug with nginx but wouldn't be surprised if they didn't prioritise it.如果你想改变这个建议用 nginx 提出一个错误,但如果他们没有优先考虑它也不会感到惊讶。

It also seems the browsers don't handle this situation very well either .似乎浏览器也不能很好地处理这种情况

HTTP/2 push is fraught with opportunities to over push and this is one example. HTTP/2 推送充满了过度推送的机会,这就是一个例子。 You should not push if client does not support WebP and you often won't know that with the information that you have at this point.如果客户端不支持 WebP,您不应该推送,而且您通常不会通过此时掌握的信息知道这一点。 Chrome seems to send webp in the accept header when you ask for the HTML for example, but Firefox does not.例如,当您询问 HTML 时,Chrome 似乎会在accept header 中发送 webp,但 Firefox 不会。

Preload is a much better, safer, option that will respect vary headers and also cache status. Preload 是一个更好、更安全的选项,它将尊重不同的标头和缓存状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM