首页 > 解决方案 > 为什么nginx不转发Apache在代理模式下发送的Vary header?

问题描述

我正在使用 Plesk(似乎是 OVH 提供的 17.8.11)并且 nginx 被配置为代理。我的 PHP 脚本在浏览器接受时将图像返回为 WEBP 格式,否则返回原始格式(JPG 或 PNG)。

在 .htaccess 中,我返回标头Vary: Accept,以便代理知道内容取决于Accept标头。

在 Plesk 的 nginx 设置中,我只选中了“代理模式”选项,其他复选框被清除。当我获取Vary: Accept不存在的图像时,我无法想象 nginx 不处理此标头,请帮我解决这个问题。

标签: apachenginxpleskovh

解决方案


I finally found the reason: I was not sending "Vary: Accept" header for ".webp" extension, only for ".jpg" and ".png". My URLs ends with .jpg or .png, never .webp and this is working good with Apache. Here was my htaccess directives:

<IfModule mod_setenvif.c>
    SetEnvIf Request_URI "\.(jpe?g|png)$" REQUEST_image
</IfModule>
<IfModule mod_headers.c>
    Header append Vary Accept env=REQUEST_image
</IfModule>

To fix it I added .webp in URLs filter:

<IfModule mod_setenvif.c>
    SetEnvIf Request_URI "\.(jpe?g|png|webp)$" REQUEST_image
</IfModule>
<IfModule mod_headers.c>
    Header append Vary Accept env=REQUEST_image
</IfModule>

Now it's all good.


推荐阅读