nginx 502 bad gateway

Update May 22, 2012: I have since moved away from TCP backend to Unix sockets. This has resolved my sporadic 502’s and gave better performance. In /etc/rc.local I removed the -a and -p arguments and replaced with -b /tmp/php.socket and in my nginx configuration, I set fastcgi_pass unix:/tmp/php.socket;

I discovered I needed a manager for fastcgi so I decided on daemontools instead of php-fpm.
Instructions for setting this up are here:  http://wiki.linuxwall.info/doku.php/en:ressources:dossiers:nginx:daemontools_spawnfcgi

I also had to modify my /etc/nginx/fastcgi_php file to reflect that I am now working on TCP port php5-cgi instead of a Unix socket.

location ~ .php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    if (-f $request_filename) {
-        fastcgi_pass unix:/var/run/www/php.sock;
+        fastcgi_pass 127.0.0.1:9000;
    }
}

This is my run command for the spawn-fcgi service:

root@node1:~# cat /etc/sv/spawn-fcgi/run
#! /bin/sh
exec /usr/bin/spawn-fcgi -n -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 5 /usr/bin/php5-cgi

Now, no more intermittent 502 Bad Gateways. This isn’t nginx’s fault, it’s just PHP crashing and there isn’t a monitor to restart the process. That has been resolved now.

One thought on “nginx 502 bad gateway

  1. Doug

    What is the rest of your config, OS, NGINX version, etc. We have had intermittent problems with 502 bad gateways for months, we are currently with php-fpm, we tried the other so called solutions, but none have worked. How sure are you of your success using the spawn-fcgi, because lots of people say php-fpm is better, but willing to try anything, got to get the webserver and php stable.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *