Nginx + PHP-FPM = 502 Bad Gateway

Since I can't add comment, I'll post it as an answer...

Check /etc/php5/fpm/pool.d/www.conf (or the file in there) if it's set to run on TCP or UNIX socket. Also check if the PHP is actually running. And look into the log files /var/log/php5-fpm.log or /var/log/php5-fpm/* and /var/log/nginx/* (depends on settings).

You should find the cause of the error there.


In my case the solutions was:

1- To Change the lister like @David:

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

On the file search listen =, comment ...sock and add 127.0.0.1:9000

;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

2- Change on Nginx Site conf (Ex. /etc/nginx/sites-available/mysite.com)

server {
       .
       .
       . 
location ~ \.php$ {
       .
       .
       .
 #fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
 fastcgi_pass 127.0.0.1:9000;
       .
       .
       .

3- Increment the timeout and max_children:

/etc/php/7.0/fpm/php.ini  =>   default_socket_timeout = 60000
/etc/php/7.0/fpm/php.ini  =>   pm.max_children = 20
/etc/php/7.0/fpm/pool.d/www.conf  =>   request_terminate_timeout = 60000

4- Incremente timeout on /etc/nginx/nginx.conf:

keepalive_timeout 65000;

After Restart php-fpm and nginx:

sudo service php7.0-fpm restart
sudo service nginx restart

I recommend before restart nginx to test if all is OK:

sudo service nginx configtest