title: Serving dynamically generated images using Nginx and FastCGI
body: <p>I am using Nginx, and need to be able to generate images on the fly.  When the client sends a request for an image, I need to run an external program to generate the image.  The external program leaves the generated image in the filesystem. </p>
  
  <p>It seems that the easiest approach would be to write a FastCGI script that runs the external program and then reads the image from the filesystem, transferring it via FastCGI to nginx.  </p>
  
  <p>However, this seems inefficient, since I would need to write my own file copy routine, and the file is copied from the disk into a local buffer, then into packets for FastCGI transfer to nginx, then into nginx's buffer, and then finally into packets to send to the client.  It seems that it would be more efficient to leverage nginx's ability to efficiently serve static content.  </p>
  
  <p>Ideally, I'd like some way to make nginx wait until the image has been generated, and then serve it from the disk.  Another thought is that maybe the FastCGI response could use some kind of header indicate that nginx should actually go and serve a file, instead of the response from the FastCGI script.  Are either of these approaches possible?</p>