title: NGINX caching upstream server when it shouldn't be
body: <p>I want to set up an NGINX server which provides the following functionality:</p>
  
  <ol>
  <li>When a request is made NGINX to get the page at /path/to/page, it fetches the page at /path/to/page.</li>
  <li>If the upstream server is down or NGINX can't connect to it for some reason, NGINX returns a cached version of the page if it has one.</li>
  <li>If the cached file is over 6 hours old, don't use it, just return a 502.</li>
  <li>If the upstream server is available, <em>never</em> use the cache.</li>
  </ol>
  
  <p>I have an NGINX config here which I think should work based on my understanding of the docs, but it doesn't and I can't see why. The problem is with point (4), this NGINX server returns the cached version of the file even if the upstream server is online.</p>
  
  <pre><code>daemon off;
  error_log /dev/stdout info;
  
  events {
  }
  
  http {
      proxy_cache_path
          "/home/jack/Code/NGINX Caching/Codebase/cache"    # Cache path
          keys_zone=cache:10m # Name of cacahe, max size for keys 10 megabytes
          levels=1:2              # Don't store all cached files in a single directory
          max_size=500m           # Max size of cache
          inactive=6h;            # Cached file deleted if not used within six hours
      proxy_cache_valid 6h;
      proxy_cache_key "$request_method$request_uri";
  
      access_log /dev/stdout;
  
      server {
          listen 8080;
  
          location ~ ^/(.+)$ {
              proxy_pass http://0.0.0.0:8000/$1;
              proxy_cache cache;
              proxy_cache_valid 6h;
              proxy_buffering on;
              proxy_cache_use_stale error timeout;
          }
      }
  }
  </code></pre>
  
  <p>Replace the <code>proxy_cache_path</code> with a path to a directory on your machine, and run another webserver on your machine on port 8000. When I modify a file served by the server on port 8000, NGINX doesn't see the change until I erase the cache. The issue is with NGINX and not my client (Firefox), even if I turn off caching in the browser, NGINX returns a 200 with the old file contents.</p>