title: Nginx - how to return a proxy generated file?
body: <p><strong>Description:</strong></p>
  
  <p>I want to implement an http server (using nginx) that serves static files.</p>
  
  <p>If the requested file doesn't exist, nginx shall send a request to a service (REST API) that will create the file and return its path.</p>
  
  <p>After that, I want nginx to return the static file that was created.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>What is the best way to return the file after its creation?</p>
  
  <p>So far I managed to do this by changing the REST API in order to return the created file path with the 302 status code and with a location header as a redirect, but I am not sure if this is a good thing to do. Is it?</p>
  
  <p>Is there any nginx-side solution for this? Do I have to create a custom module?</p>
  
  <p><strong>Conf file:</strong></p>
  
  <pre><code>http {
      server {
          listen 80;
  
          location /files {
              try_files $uri @rest;
          }
  
          location @rest {
              rewrite ^(.*)$ /api/ break;
              proxy_set_header Host      $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_pass       http://localhost:8080;
          }
      }
  }
  </code></pre>