nginx proxy caching not working

I an proxying to an external JSON api and am trying to cache the proxy responses. To determine if the proxy cache is working I have added

add_header X-Cached $upstream_cache_status;

and always see a MISS value.

My nginx config location:

location /api/tides {
  proxy_hide_header Cache-Control;
  proxy_ignore_headers Cache-Control;
  proxy_cache worldtidecache;
  proxy_set_header Host www.worldtides.info;
  proxy_pass https://example.com/api/$query_string;
  add_header X-Cached $upstream_cache_status;
}

Prior to that I have proxy_cache_path set

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=worldtidecache:100m max_size=1g inactive=48h use_temp_path=off;

www-data has read/write access to /var/cache/nginx

I am assuming that proxy_buffering is set to on as I don't have any explicit setting for this.

The nginx config is valid. I have tested it with nginx -c nginx.conf -t:

╰─sudo nginx -c nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

I've tailed both the error.log and access.log and I don't see anything that would indicate why the caching wouldn't be working.

The headers I get from the proxy response:

Server: nginx/1.11.13
Date: Sun, 20 Aug 2017 14:01:59 GMT
Content-Type: text/json; charset=utf-8
Content-Length: 2976
Connection: keep-alive
Content-Encoding: gzip
X-Cloud-Trace-Context: 38faabaa5ada170536632bb55a0ddf00;o=1
Vary: Accept-Encoding
X-Cached: MISS

I'm stuck at this point. I don't know what else would be causing nginx to not cache. There isn't any cache busting happening on the requests like a unique query param, the url is consistent on each request. Outside of setting the header to the cache attempt result I don't know of any other way to log what the proxy caching is doing.

0
задан 20 August 2017 в 17:05
2 ответа

Twitter приходит на помощь: https://twitter.com/btucker/status/899309118137057286

proxy_cache_valid any 48h;
3
ответ дан 4 December 2019 в 12:19

Второе решение после того, как @bcardarella мне не помогло

Добавить proxy_buffering в свой блок местоположения

Источник: https://github.com/nginx-proxy /nginx-proxy/issues/800#issuecomment-383901151

0
ответ дан 24 March 2020 в 20:49

Теги

Похожие вопросы