# 502 Bad Gateway (php-fastcgi processes crashing)

**URL:** <https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001>\
**Category:** NGINX\
**Created:** [February 10, 2011, 11:52am UTC](https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001 "2011-02-10T11:52:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 10, 2011, 11:52am UTC](https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001/1 "2011-02-10T11:52:30Z")

</div>

Our site is currently on a linux VPS with DreamHost and using nginx  
0.8.53 as the web server and php-fcgi version 5.2.15. On occasion, our  
php-fastcgi processes just suddenly crash, and our users are left with a  
“502 Bad Gateway” error from nginx. The server is configured to use unix  
sockets and not a TCP port.

Our php-fastcgi settings are the following:  
PHP\_FCGI\_CHILDREN = 8 (should be plenty for our traffic)  
PHP\_FCGI\_MAX\_REQUESTS = 400 (used to be 1000, we tried lowering to see  
if it would help, and nada.)

The http error logs show these sort of error messages:

> > _9 connect() to unix:/home/_\*\*/.php.sock failed (111: Connection  
> > refused) while connecting to upstream, client: \*\*\*\*\*, server: \*\***,  
> > request: "GET _, upstream: "fastcgi://unix:/home//.php.sock:"  
> > 28898 connect() to unix:/home/_**/.php.sock failed (11: Resource  
> > temporarily unavailable) while connecting to upstream, client: \*\*,  
> > server: \*_, request: "GET **", upstream:  
> > "fastcgi://unix:/home/**_/.php.sock:"

We wrote a daemon that restarts the nginx web server when it detects the  
processes crash, or these error codes pop up, but sometimes restarting  
the web server fails to bring back the php5.cgi processes, and our users  
are left without any access to our site material for a while. This  
approach is also pretty sloppy and doesn’t work sometimes.

Why would php lock up and crash like this, and why isn’t nginx  
automatically reload the php5.cgi processes? Is there another way to  
serve php to our users that doesn’t use php-fastcgi? (I heard people  
mention php-fpm, but I’m not too familar with using it or its  
advantages). We think that it might be the php5.cgi processes timing out  
waiting for MySQL queries to finish processing, since we can sometimes  
crash php5.cgi simply by running long queries on the web site. Any  
suggestions on settings we can tweak?

Thanks to anyone who can help.

Posted at Nginx Forum:

> **[502 Bad Gateway (php-fastcgi processes crashing)](https://forum.nginx.org/read.php?2,173776,173776#msg-173776)**
>
> Our site is currently on a linux VPS with DreamHost and using nginx 0.8.53 as the web server and php-fcgi version 5.2.15. On occasion, our php-fastcgi processes just suddenly crash, and our users are left with a "502 Bad Gateway" error from...

---

<div class="post-metadata">

**Author:** ![shadowlink64](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@shadowlink64](https://www.ruby-forum.com/u/shadowlink64)\
**Post date:** [February 10, 2011, 12:12pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001/2 "2011-02-10T12:12:37Z")

</div>

On Thu, Feb 10, 2011 at 5:51 PM, shadowlink64 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> serve php to our users that doesn’t use php-fastcgi? (I heard people  
> mention php-fpm, but I’m not too familar with using it or its  
> advantages).

guess what, the advantage is it solves your problem.

---

<div class="post-metadata">

**Author:** ![shadowlink64](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@shadowlink64](https://www.ruby-forum.com/u/shadowlink64)\
**Post date:** [February 10, 2011, 12:15pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001/3 "2011-02-10T12:15:08Z")

</div>

On 2/10/11 5:51 AM, shadowlink64 wrote:

> The http error logs show these sort of error messages:
> 
> > > _9 connect() to unix:/home/_\*\*/.php.sock failed (111: Connection  
> > > refused) while connecting to upstream, client: \*\*\*\*\*, server: \*\***,  
> > > request: "GET _, upstream: "fastcgi://unix:/home//.php.sock:"  
> > > 28898 connect() to unix:/home/_**/.php.sock failed (11: Resource  
> > > temporarily unavailable) while connecting to upstream, client: \*\*,  
> > > server: \*_, request: "GET **", upstream:  
> > > "fastcgi://unix:/home/**_/.php.sock:"

This is a PHP problem and not related to nginx. I’ve never been happy  
using php-fastcgi as a backend without a process manager of some sort.

Your choices include using PHP-FPM, using Supervisord, using another  
process manager, or of course, using Apache as a backend to handle PHP  
requests only with nginx serving static content and acting as a reverse  
proxy the same way you use it now.

If your code will support it, and you want to use PHP-FPM, then  
upgrading to PHP 5.3.x may be a good idea as no patching is required to  
build from sources.

Additionally, I have seen _anecdotal_ reports here and elsewhere that  
having your processes listen on a TCP port rather than a Unix socket may  
be beneficial.

> We wrote a daemon that restarts the nginx web server when it detects the  
> processes crash, or these error codes pop up, but sometimes restarting  
> the web server fails to bring back the php5.cgi processes, and our users  
> are left without any access to our site material for a while. This  
> approach is also pretty sloppy and doesn’t work sometimes.
> 
> Why would php lock up and crash like this, and why isn’t nginx  
> automatically reload the php5.cgi processes?

nginx didn’t start the processes and does not control them.

> Is there another way to  
> serve php to our users that doesn’t use php-fastcgi? (I heard people  
> mention php-fpm, but I’m not too familar with using it or its  
> advantages).

See above. It’s a process manager and will restart dead children  
automatically.

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![shadowlink64](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@shadowlink64](https://www.ruby-forum.com/u/shadowlink64)\
**Post date:** [February 11, 2011, 6:40pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-php-fastcgi-processes-crashing/203001/4 "2011-02-11T18:40:57Z")

</div>

On Thu, 10 Feb 2011 05:51:06 -0500, shadowlink64 wrote:

> Our site is currently on a linux VPS with DreamHost and using nginx  
> 0.8.53 as the web server and php-fcgi version 5.2.15. On occasion,  
> our  
> php-fastcgi processes just suddenly crash, and our users are left  
> with a  
> “502 Bad Gateway” error from nginx. The server is configured to use  
> unix  
> sockets and not a TCP port.

Everybody has that problem with PHP. The solution is to monitor PHP  
with _something_ (php-fpm, supervise, etc…)

I do it with supervise:  
[http://wiki.linuxwall.info/doku.php/en:ressources:dossiers:nginx:daemontools\_spawnfcgi](http://wiki.linuxwall.info/doku.php/en:ressources:dossiers:nginx:daemontools_spawnfcgi)

Julien
