skip to Main Content

Nginx cache with proxy_cache_background_update on;
some api response that has Cache-Control=max-age=7, public, stale-while-revalidate=59. After a while upstream response changes header for the same resource to Cache-Control=private, no-cache, must-revalidate. Nginx will continue to serve old stale content and never return new non-cachable response.

Is it possible return new response and not staled cached response when headers change?

#default_location
proxy_cache_background_update on;
proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504 http_429;
proxy_cache_lock on;

2

Answers


  1. you can try proxy_cache_bypass

    see the below guide it will help you to configure it:

    https://www.claudiokuenzler.com/blog/584/nginx-configure-bypassing-purging-proxy-cache-ngx_cache_purge

    Login or Signup to reply.
  2. If I understand correctly, You want to bypass Nginx cache based on cache-control headers from upstream.

    For that, get the value of cache-control, if it contains no-cache directive then set proxy_cache_bypass to 1. Here’s the config,

    http{
    
      map $http_cache_control $bypass_cache {
         no-cache   1;
         default 0; #if no-cache directive is not present then serve from cache
      }
    
      server{
         proxy_cache_bypass $bypass_cache;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search