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;