大网数据广告活动
服务器运维问答

nginx 1.8.1 is not caching with Vary Accept header

· 52CE 我爱测

I have a strange nginx caching behavior. There are 2 responses. The first one had stopped caching when I updated server from 1.6 to 1.8.1, another one is caching like before. The first one (broken) has following response headers:…

问题原文

原帖链接 · dzmitry · CC BY-SA 3.0

I have a strange nginx caching behavior. There are 2 responses. The first one had stopped caching when I updated server from 1.6 to 1.8.1, another one is caching like before. The first one (broken) has following response headers:

Allow →GET, HEAD, OPTIONS

Connection →keep-alive

Content-Encoding →gzip

Content-Language →ru

Content-Type →application/json

Date →Tue, 15 Mar 2016 07:31:53 GMT

Server →nginx/1.8.1

Transfer-Encoding →chunked

Vary →Accept,Accept-Language,Cookie,Accept-Encoding

X-Frame-Options →SAMEORIGIN

Another response (which works as I expect) has following headers:

Connection →keep-alive

Content-Encoding →gzip

Content-Language →ru

Content-Type →application/json

Date →Tue, 15 Mar 2016 07:32:40 GMT

Server →nginx/1.8.1

Transfer-Encoding →chunked

Vary →Accept-Language,Cookie,Accept-Encoding

X-Frame-Options →SAMEORIGIN

There are no cookies or Accept header in the first case, but only

proxy_ignore_headers Vary

saved the situation. It works well with it. Looks like some magic. But what was wrong, what am I missing? Below are the current settings:

proxy_pass    http://localhost:7050;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
limit_req zone=banka burst=5 nodelay;
proxy_cache   cache;
proxy_cache_valid 5m;
proxy_ignore_headers Vary;
    

已采纳解答

原帖链接 · Aventador2016 · CC BY-SA 3.0

Don't know if you fixed it, will drop this here in case someone needs it.

In the source code of Nginx there is set a maximum of 42 characters being used by Vary headers. In my case there where 51 characters thus my Vary headers where being handled as Vary:* (no-cache). Setting the maximum to 84 fixed it for me.

This article explains it more in depth.

https://thedotproduct.org/nginx-vary-header-handling/

Credits to the guy posting that short article.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。