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

can I config my nginx to run some shell command/script if encounter 5xx error?

My spawn-fcgi crashes often, I have a cronjob restart it periodically, but I think the best solution would be if nginx fail to reach spawn-fcgi then restart it via command.

share|improve this question

3 Answers 3

You could run monit doing like this in the config:


check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid
        start program = "/etc/init.d/spawn-fcgi start"
        stop program = "/etc/init.d/spawn-fcgi stop"
        if failed host localhost port 80 protocol HTTP request /test.php then restart
        if 5 restarts within 5 cycles then timeout
        depends on nginx

check process nginx with pidfile /var/run/nginx.pid
        start program = "/etc/init.d/nginx start"
        stop program = "/etc/init.d/nginx stop"
        if failed host localhost port 80 protocol HTTP request /token.html then restart
        if 5 restarts within 5 cycles then timeout

the test.php is just a php file that does echo 'OK'

Of course I supose you already have a spawn-fcgi init script and a PID file.

share|improve this answer

No, you can not, without patching nginx source, and that would be wrong because nginx can encounter 5xx error on numerous reasons, not only spawn-fcgi crash.

Just run spawn-fcgi under process supervisor, for example daemon tools or supervisord, which you can configure to restart spawn-fcgi when it crashes.

share|improve this answer
    
thanks, but my problem is somehow odd, the spawn-fcgi process is still there, but nginx just can't communicate with it. –  est Nov 17 '10 at 9:40
    
I suggest you to go either php5-cgi under supervisor or php-fpm (if you are using PHP). If you're using ruby or python, there's uwsgi and rack :-) –  Kristaps Nov 17 '10 at 12:16

You could write an script that connects to your server on port 80 (telnet/curl) and searches for a specified syntax and if it's not available restarts the daemon. You could take it out of the serverheader I guess?

share|improve this answer
    
How that is different on restarting spawn-fcgi in cron in terms of avoiding 502 error ? –  Kristaps Nov 16 '10 at 10:39
    
Not, other than restarting it only when it encounters the error. It's better to avoid the error at all. –  Mark Nov 16 '10 at 12:32

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.