Take the 2-minute tour ×

I get a 502 Bad Gateway with nginx when using spawn fcgi to spawn php5-cgi.

I use this to span an instance on server start using the following line in rc.local


presumably I'm getting the error because the spawn-fcgi / php5-cgi dies and there is nothing listening there anymore to parse php.

I get nothing in the logs that I can see anywhere, I'm out of ideas (and new to this setup with nginx)

share|improve this question
First, don't give up! nginx is a wonderful piece of software. Try with php-fpm. Don't miss the /etc/php5/fpm dir with settings inside and note the copy of php.ini that may override your own php.ini config... Configure the logs so you can have a look if it fails. –  ring0 Nov 23 '10 at 3:08
Try logging into the server as root and type /usr/bin/spawn-fcgi -a -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid into the command prompt. (That's the same line that's in your rc.local.) Do you get any error messages? –  kijin Nov 23 '10 at 3:59
I believe this question would be more appropriate (and get more answers) on serverfault.com. –  netcoder Nov 23 '10 at 5:04
Thanks netcoder, that's a good idea. I'll move it there. –  Joel Wickard Nov 23 '10 at 13:51
Does this give you any clues? tail /var/log/nginx/error.log –  e.thompsy Jul 16 at 18:11

10 Answers 10

I got the same problem. I executed my localhost and the page displayed the 502 bad gateway message. This helped me:

Go to /etc/php5/fpm/pool.d/www.conf and edit the line listen = /var/run/php5-fpm.sock to listen =

Maybe it will help you.

Source from: http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm

share|improve this answer
This one actually helpe –  BreakDS May 28 '13 at 16:59

The 502 error appears because nginx cannot hand off to php5-cgi. You can try reconfiguring php5-cgi to use unix sockets as opposed to tcp .. then adjust the server config to point to the socket instead of the tcp ...

netstat -an | grep 9000 # is the port open? 
share|improve this answer
What is the process if the port is not open? –  Adripants Oct 30 '12 at 4:39
Start the service? –  sdolgy Oct 31 '12 at 5:54
In my case, service is started but there is nothing in the result of netstat -an | grep 9000???? –  shgnInc Mar 5 at 5:25
What port do you have it configured to run on? 9000 was based on the command line argument above (-p 9000) –  sdolgy Mar 6 at 15:09
I have the same problem as @shgnInc, I've got 4 sites, the first 3 run on 9000 - 9002, but the 4th site is configured in the nginx and php-fpm conf files to use port 9003, and I've restarted both services but I see nothing for netstat -an | grep 9003 –  Willshaw Media Jun 20 at 11:26

If running a linux server, make sure that your IPTABLES configuration is correct.

Execute sudo iptables -L -n , you will recieve a listing of your open ports. If there is not an Iptables Rule to open the port serving the fcgi script you will receive a 502 error. The Iptables Rule which opens the correct port must be listed before any rule which categorically rejects all packets (i.e. a rule of the form "REJECT ALL -- reject-with icmp-port-unreachable or similar)

On my configuration, to properly open the port, I had to execute this command (assume my fcgi server is running at port 4567):

sudo iptables -I INPUT 1 -p tcp --dport 4567 -j ACCEPT

WARNING: This will open port 4567 to the whole world.

So it might be better to do something like this:

   sudo iptables-save >> backup.iptables
   sudo iptables -D INPUT 1 #Delete the previously entered rule
   sudo iptables -I INPUT 1 -p tcp --dport 8080 -s localhost -j ACCEPT # Add new rule

Doing this removed the 502 error for me.

share|improve this answer

Go to /etc/php5/fpm/pool.d/www.conf and if you are using sockets or this line is uncommented

listen = /var/run/php5-fpm.sock

Set couple of other values too:-

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Don't forget to restart php-fpm and nginx. Make sure you are using the same nginx owner and group name.

share|improve this answer

When I did sudo /etc/init.d/php-fpm start I got the following error:

Starting php-fpm: [28-Mar-2013 16:18:16] ERROR: [pool www] cannot get uid for user 'apache'

I guess /etc/php-fpm.d/www.conf needs to know the user that the webserver is running as and assumes it's apache when, for nginx, it's actually nginx, and needs to be changed.

share|improve this answer


fastcgi_pass    unix:/var/run/php-fpm.sock;


fastcgi_pass    unix:/var/run/php5-fpm.sock;
share|improve this answer

Similar setup here and looks like it was just a bug in my code. At the start of my app I looked for the offending URL and this worked: echo '<html>test</html>'; exit();

In my case, turns out the problem was an uninitialized variable that only failed under peculiar circumstances.

share|improve this answer

You can make nginx ignore client aborts using:

location / {
  proxy_ignore_client_abort on;
share|improve this answer

You have to match the settings for PHP-FPM and Nginx to communicate over sockets or TCP.

So go to /etc/php5/fpm/pool.d/www.conf and look for this line:

listen = /var/run/php5-fpm.sock

Then go to /etc/nginx/nginx.conf

Look for this:

upstream php { server unix:/var/run/php5-fpm.socket; }

Match those values and you should be all set.

share|improve this answer

Try disabling the xcache or apc modules. Seems to cause a problem with some versions are saving objects to a session variable.

share|improve this answer

Your Answer


