views:

40

answers:

2

Is it possible to configure lighttpd so that a request for a file succeeds if the file exists, but is handled and redirected, for example to a cgi script, if the file does not exist?

What I'm trying to achieve is having a set of image files on disk which are generated by a script and served directly. On a request, if the file does not exist, the script will generate the image and save it to disk (for future requests) and then either serve the image directly or redirect back to the same URL which will this time succeed. I'm essentially caching the generated output on disk.

I currently have a prototype in which the script always handles the request, reading and echoing the file if it exists, but I'd rather save the overhead and have lighttpd serve it directly if possible.

+1  A: 

You can have the best of both worlds. Lighttpd will serve the file if you give it a

X-Sendfile: path to file

see: http://redmine.lighttpd.net/wiki/1/X-LIGHTTPD-send-file. There's a php example on the documentation page.

Kyle Butt
Perfect thanks!
Joe
+1  A: 

You can set the:

server.error-handler-404 

config option to a script which will do what you want.

see http://redmine.lighttpd.net/wiki/1/Server.error-handler-404Details

Perry