1

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.

est
  • 181
  • 1
  • 8

3 Answers3

2

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.

aline
  • 21
  • 1
0

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.

Kristaps
  • 2,985
  • 17
  • 22
  • 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 09: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
-2

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?

Mark
  • 323
  • 2
  • 5
  • 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