Nginx – Getting 502 bad gateway after updating php to 5.5

nginxPHPphp-fpm

On my ubuntu 12.10 server I upgraded php to 5.5. After getting 502 errors on my wordpress site I did some googling and discovered I need to change my nginx configs to match the passing of php scripts to php5-fpm.sock rather than port 9000. So I changed my site's config file to the below:

 # Pass PHP scripts on to PHP-FPM
    location ~* \.php$ {
        try_files       $uri /index.php;
        fastcgi_index   index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

I then did service nginx restart. But the 502 error persists.

Upon checking the error log I get:

2014/03/30 14:16:37 [error] 1451#0: *21 connect() failed (111: Connection refused) while connecting to upstream, client: 81.107.86.251,, server: www.harryg.me, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "harryg.me"

So it looks like php-fpm is trying to pass stuff to fastcgi://127.0.0.1:9000. Why isn't it obeying the config file change?

edit:

My /etc/php5/fpm/pool.d/www.conf has listen = /var/run/php5-fpm.sock in it.

Best Answer

I had this problem too, and I solved it by using TCP connections. Quoting this answer Error 502 in nginx + php5-fpm (found following the link from danmash):

The issue is socket itself, its problems on high-load cases is well-known. Please consider using TCP\IP connection instead of unix socket, for that you need to make these changes:

  • in php-fpm pool configuration replace listen = /var/run/php5-fpm.sock with listen = 127.0.0.1:7777
  • in /etc/nginx/php_location replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass 127.0.0.1:7777;

In your case it would be 127.0.0.1:9000.