Home » Php » Drupal + Nginx + Php-cgi : 502 Bad Gateway error

Drupal + Nginx + Php-cgi : 502 Bad Gateway error

Posted by: admin July 12, 2020 Leave a comment


We are running Drupal 5.x on Nginx with php-fastcgi.
Things were working fine for a while. All of a sudden, we (users) are running into 502 Bad Gateway error.
Restarting PHP-cgi, nginx.. rebooting machine etc did not help.

Did anyone else run into this type of issue? What are the possible suspects?

How to&Answers:

Today I was getting “502 Bad Gateway” on a CI project , after digging into the problem I found out it is a problem of nginx fastcgi buffers , here is how to fix it :
open /etc/nginx/nginx.conf

add the following lines into http section :

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;


502 errors are usually caused when PHP freaks out for some reason. The first thing you should do is check all of your logfiles. That includes system stuff in /var/log/ and the nginx logs.

If you haven’t made any recent changes and the problem just started happening for no apparent reason, PHP may be running out of memory. I know when it happens as an Apache module it gives a blank screen–wouldn’t be surprised if a 502 error happened under nginx and the FastCGI interface. That’s easy to fix by putting ini_set(‘memory_limit’, ‘256M’) into your index.php and see if that fixes the problem.

Also, can you load stand alone PHP files that don’t involve Drupal? Put putting <?php phpinfo(); ?> into a file called info.php and try hitting that and see what happens.

Good luck!


I got this error as well and I eventually disable all modules (non-core) and enable them one by one to see what caused the error.

Here’s a easy way to disable all non-core modules.


If the problem just started.. Read above.. if the server is new just setup, try this command to see if it’s even listening on it’s port

netstat -lpn | grep ":9000"

If course you could have setup fast-cgi to work on a different port, so just replace the port 9000 in that statement with what ever port you’re looking for.. If nothing continues to show up, likely nothing is listening on that port and you need to fix that problem first.


If you have firePHP disable it.
Big headers causes problems while nginx comunication with php


Usually when I have come across this it has been a fatal error in PHP somewhere. Have a look at your PHP-cgi log to see if it is in there. There should be something in the nginx log like this: 104: Connection reset by peer. Depending on your setup this (sorry, link dead) might help but if you’re using php-fpm it won’t.


increase your memory limit and it will be fixed. ini_set(‘memory_limit’, ‘256M’);


It is because you probably upgraded to PHP 5.5 and therefore you now are using opcode cache which you may have enabled twice. That is check php.ini and also opcache.ini.