Score:1

proxy_cache_bypass keeps serving old cached response if URL changes to non-cacheable

md flag

Under normal circumstances, when using proxy_cache_bypass nginx will fetch a fresh copy from upstream, and overwrite the cached response with the new one.

But if the URL changes from a cacheable to a non-cacheable response (for example to a 4xx response with Cache-Control: no-cache), then using proxy_cache_bypass will indeed serve a fresh copy from upstream, but it will leave the old copy in the cache.

Which means that everytime the URL is requested without triggering proxy_cache_bypass, it will keep serving the old cache.

I guess this is an intended behaviour, because proxy_cache_bypass only overwrites cached responses by saving a new one, and a no-cache response means there is nothing to save? Is this what's happening?

How can I solve this? I don't want to enable caching for 4xx responses...

I'm running nginx/1.14.2

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my-cache:70m max_size=28g inactive=1d;
proxy_temp_path /var/cache/nginx/tmp;
proxy_cache my-cache;
proxy_cache_key $remote_user$scheme$host$request_uri;

proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_429;

proxy_cache_bypass $http_cache_control;

proxy_read_timeout 90;
add_header X-Cache-Status $upstream_cache_status;

etag off;
Score:0
us flag

You can tell nginx to cache only 200, 301 and 302 responses by specifying

proxy_cache_valid 10m;

This tells nginx to cache valid responses for ten minutes.

md flag
Yes, but I want to use higher caching times. Right now I'm setting it at 24h. Besides, I think `Cache-Control` overrides `proxy_cache_valid`
us flag
You can specify any time with `proxy_cache_valid`. The main point of this directive is that it limits caching to only these response statuses. Other status codes are not cached.
md flag
I did that and it didn't change anything. The problem is not that it's caching unwanted status codes, but that responses that change from a cacheable to a non cacheable `Cache-Control`, keep serving the cached response.
Score:0
vn flag
Zhe

This is the expected behavior. That's why they want you to use the paid version to get the proxy_cache_purge directive

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.