I need to make a server work this way:

Check if the requested file exists locally.

  • if is there, serve it right away
  • if not, pass the REQUEST_URI to a script which will download it
  • if the file has ?delete=1, call the script to remove it

I had successfully implemented this behavior with nginx (using the 404 error handler) and PHP. The thing is that to implement this on another server I have to install/compile both nginx and php.

My question is if there is a way to remove php from the stack and put up nginx (or some other server) to do the job. An ideal way for me would be if I could run/call bash scripts/commands directly from the server.

Does anyone can point me to the right direction?


What you are trying to do can be accomplished with plain ol' CGI and a shell script very easily. How you enable/implement CGI depends on the server. Nginx should be capable, but I know Apache is. Refer to the documentation or ask a specific question about enabling CGI on a given web server.

Simple bash logic to test for a whether a file exists and display it:



/bin/echo "Content-type: text/plain"

if [ -f $FILE ]; then
  /bin/cat $FILE
  /usr/bin/wget $URI

You can parse ${QUERY_STRING} to get your delete action. I'll leave the full solution and error handling as an exercise to you. It gets a little tricky and is beyond the scope of our Q&A format.

See: Parsing of QUERY_STRING in Bash CGI Scripts

  • 1
    Thank me with your upvote and an accepted answer if it ends up working for you. :) – Aaron Copley Aug 24 '12 at 22:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.