PHP-FPM and Nginx: 502 Bad Gateway PHP-FPM and Nginx: 502 Bad Gateway php php

PHP-FPM and Nginx: 502 Bad Gateway


If anyone finds this page by encountering the same problem I had, I found the answer here.

For those of you who can't be bothered to click and work it out for themselves... ;)

The Condition:

Ubuntu or Debian server with NGINX and PHP 5.3 works fine but upgrading PHP to 5.4 gives 502 Bad Gateway errors. Looking for services running on port 9000 (typically running netstat -lp or similar) returns nothing.

The fix:

Open /etc/php5/fpm/pool.d/www.conf and make a note of the 'listen' parameter (in my case /var/run/php5-fpm.sock):

; The address on which to accept FastCGI requests.; Valid syntaxes are:;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on;                            a specific port;;   'port'                 - to listen on a TCP socket to all addresses on a;                            specific port;;   '/path/to/unix/socket' - to listen on a unix socket.; Note: This value is mandatory.listen = /var/run/php5-fpm.sock

and replace the fastcgi_pass variable in your vhost with the location you just noted.

So this sample symfony2 configuration (taken from here):

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  location ~ ^/(app|app_dev)\.php(/|$) {    fastcgi_pass   127.0.0.1:9000;    fastcgi_split_path_info ^(.+\.php)(/.*)$;    include fastcgi_params;    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;    fastcgi_param  HTTPS              off;  }

becomes this:

  # pass the PHP scripts to FastCGI server at /var/run/php5-fpm.sock  location ~ ^/(app|app_dev)\.php(/|$) {    fastcgi_pass unix:/var/run/php5-fpm.sock;    fastcgi_split_path_info ^(.+\.php)(/.*)$;    include fastcgi_params;    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;    fastcgi_param  HTTPS              off;  }

Then restart nginx:

sudo /etc/init.d/nginx restart

Note: replace ~ ^/(app|app_dev)\.php(/|$) { with ~ ^/index\.php(/|$) { if you're not on SF2**

Hope this saves someone a little bit of time :)

Edit

Of course, you could change the listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000 in /etc/php5/fpm/pool.d/www.conf then restart php5-fpm (which would save you from having to change your vhosts), but you have to assume they changed php5-fpm to run through a socket rather than listening on port 9000 for a reason.

Edit2

If you're still experiencing 502 error see this answer.


Try setting these values, it solves problem in fast-cgi

fastcgi_buffer_size 16k;fastcgi_buffers 4 16k;


I made all this similar tweaks, but from time to time I was getting 501/502 errors (daily).

This are my settings on /etc/php5/fpm/pool.d/www.conf to avoid 501 and 502 nginx errors…The server has 16Gb RAM. This configuration is for a 8Gb RAM server so…

sudo nano /etc/php5/fpm/pool.d/www.conf

then set the following values for

pm.max_children = 70pm.start_servers = 20pm.min_spare_servers = 20pm.max_spare_servers = 35pm.max_requests = 500

After this changes restart php-fpm

sudo service php-fpm restart