2

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;
2

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.

  • Looks like this is the reason: the first header has 46 characters. Unfortunately I don't have access to that server anymore to check it. – dmitry Nov 24 '16 at 9:51
  • Looks like this was updated to 128, according to this ticket – ESG May 23 '17 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.