title: How I can force nginx to save response in a cache folder, with headers from upstream server?
body: <p>I use Django REST Framework for my API, nginx as a reverse proxy and redis for caching some static api data.<br>
  I was trying to implement caching with <code>Cache-Control: max-age</code> and <code>Last-Modify</code> headers.<br>
  In a nutshell it looks like this:  </p>
  
  <pre><code>class SomeViewSet(viewsets.ModelViewSet):
      ....
      def list(self, request, *args, **kwargs):
          queryset = self.get_queryset()
          cache_key = self._get_cache_key() # get a key for reddis
          response = self._get_data_from_cache(cache_key) # get a data from reddis
          if response:
              # If data in redis return Response with a same Last-Modify
              # and 'Cache-Control': 'max-age=120'
              return response
  
          # Setting up new value for this viewset in a reddis
          serializer = self.get_serializer(queryset, many=True)
          now = datetime.datetime.now()
          cache.set(cache_key, [now, serializer.data])
          return Response(serializer.data, headers={'Last-Modified': now, 'Cache-Control': 'max-age=120'})
  </code></pre>
  
  <p>It work like I expect, browser caching data for 120 seconds and when it expire client check content with an <code>If-Modified-Since</code> header.<br>
  But I though when I setting up <code>max-age</code> headers nginx will save it in a cache folder and will serving up all clients without hitting a server.<br>
  here is my testing nginx config from local machine:  </p>
  
  <pre><code>upstream django {
    server 127.0.0.1:8002; 
  }
  
  proxy_cache_path /home/ivan/projects/kors/test_prod/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
  
  server {
      listen      8000;
      server_name 127.0.0.1; #
      charset     utf-8;
      client_max_body_size 75M;
  
  location /media  {
      alias /path/to/media; 
      expires 1y;
      log_not_found off;
      access_log off;
  }
  location /static {
      alias /path/to/static; 
      expires 1y;
      log_not_found off;
      access_log off;
  }
  
  location / {
      uwsgi_pass  django;
      include     /path/to/uwsgi_params; 
      uwsgi_param Host $host;
      uwsgi_param X-Real-IP $remote_addr;
      uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
      uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
  
      proxy_cache my_cache;
      }
  }
  </code></pre>
  
  <p>Consider this article <a href="https://www.nginx.com/blog/nginx-caching-guide/" rel="nofollow noreferrer">nginx-caching-guide</a>.  </p>
  
  <blockquote>
    <p>How Does NGINX Determine Whether or Not to Cache Something?<br>
    By default, NGINX respects the Cache‑Control headers from origin servers. It does not cache responses with Cache‑Control set to Private, No‑Cache, or No‑Store or with Set‑Cookie in the response header. NGINX only caches GET and HEAD client requests. You can override these defaults as described in the answers below.</p>
  </blockquote>
  
  <p>I thought a <code>Cache-Contol: max-age</code> header will force nginx to save json in a caching folder.<br>
  Folder was created but there is no data here and all my requests from separate browsers are hitting a server.<br>
  What am I missed? Or maybe I was totally misunderstood a concept of caching with nginx?  </p>