Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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/bash

FILE="/path/to/file"
URI="http://example.com/file"

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

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

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

share|improve this answer
    
thanks for the tip –  adrian7 Aug 24 '12 at 22:22
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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