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
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
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)
I executed my
localhost
and the page displayed the502 bad gateway
message. This helped me:/etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock
tolisten = 127.0.0.1:9000
sudo service php5-fpm restart
Maybe it will help you.
Source from: http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm
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 -- 0.0.0.0/0 0.0.0.0/0 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):
WARNING: This will open port 4567 to the whole world.
So it might be better to do something like this:
Doing this removed the 502 error for me.
I had the same problem while setting up an Ubuntu server. Turns out I was having the problem due to incorrect permissions on socket file.
If you are having the problem due to a permission problem, you can uncomment the following lines from: /etc/php5/fpm/pool.d/www.conf
Alternatively, although I wouldn't recommend, you can give read and write permissions to all groups by using the following command.
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.
Hope this tip will save someone else's life. In my case the problem was that I ran out of memory, but only slightly, was hard to think about it. Wasted 3hrs on that. I recommend running:
or
...along with running problematic requests on the server to see if your memory doesn't run out. And if it does like in my case, you need to create a swap file (unless you already have one).
I have followed this tutorial to create swap file on Ubuntu Server 14.04 and it worked just fine: http://www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/
change
to