502 Bad Gateway – Nginx and PHP-FPM

Nginx 502 Bad Gateway cost me more time than the actual Ubuntu + PHP-FPM + MySQL installation I was working on. In my case the 502 Bad Gateway error on my installation was the result of a parameter mismatch between /etc/ningx/sites-available/default and /etc/php5/fpm/pool.d/www.conf.

In order to solve the 502 Bad Gateway problem edit the Nginx file /etc/ningx/sites-available/default.

nano /etc/ningx/sites-available/default

And look for the commented line # fastcgi_pass unix:/var/run/php5-fpm.sock; and uncomment the line.

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

You location ~ .php$ { section should look similar to mine.

 location ~ .php$ {
                fastcgi_split_path_info ^(.+.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #       # With php5-cgi alone:
        #       fastcgi_pass;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

The final two steps are to restart Nginx and PHP-FPM.

sudo service nginx restart
sudo service php5-fpm restart

Now you have a working installation like it should have been from the beginning.

  • Dan

    Thanks! saved me 🙂

  • Byron

    Thanks a lot saved me a lot of time!