title: Nginx Reverse Proxy Following Redirects
body: <p>I would like Nginx to return actual files instead of response with <code>Location:</code> redirect header. I use Nginx as a reverse proxy cache: <code>cdn.mydomain.com</code> receives a request and contacts <code>api.mydomain.com/files/</code> to fetch image files from there, but <code>api.mydomain.com/files/</code> returns a blank response with <code>Location:</code> redirect header to AWS S3, instead of a file itself.</p>
  
  <p>Thus, Nginx caches the blank redirect response. How can I make Nginx to fetch and cache the actual file from S3.</p>
  
  <pre><code>user www-data;
  worker_processes 4;
  pid /var/run/nginx.pid;
  
  http {
      server_names_hash_bucket_size 64;
      proxy_redirect off;
  
      proxy_cache_path /var/cache/nginx levels=2:2:2 keys_zone=my-cache:8m max_size=4G inactive=600m;
      proxy_temp_path /var/cache/tmp;
  
      server {
          listen 80;
          server_name cdn.mydomain.com;
          server_tokens off;
          location / {
              proxy_pass http://api.mydomain.com/files/;
  
              proxy_cache my-cache;
              proxy_cache_valid 200 302 30d;
              expires 30d;
              add_header Pragma public;
              add_header Cache-Control "public";
          }
  }
  </code></pre>