# Php exits with 502 Bad Gateway

**URL:** <https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002>\
**Category:** NGINX\
**Created:** [February 19, 2012, 3:26am UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002 "2012-02-19T03:26:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 19, 2012, 3:26am UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/1 "2012-02-19T03:26:33Z")

</div>

Hello

I have a problem where php exits with a 502 Bad Gateway error every once  
in a while. It appears to be unpredictable. When it happens I have to  
re-start the vm – restarting nginx doesn’t fix it. The OS is Ubuntu  
running on VirtualBox and I am connecting to it over a bridged network  
only from the host Vista machine. This is a portion of the error log  
and also the configuration. Any help will be appreciated.

2012/02/18 18:12:31 [debug] 723#0: accept on 0.0.0.0:443, ready: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 accept: 192.168.1.69 fd:12  
2012/02/18 18:12:31 [debug] 723#0: \*1031 event timer add: 12:  
60000:2472717397  
2012/02/18 18:12:31 [debug] 723#0: \*1031 epoll add event: fd:12 op:1  
ev:80000001  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_do\_handshake: -1  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_get\_error: 2  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL handshake handler: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_do\_handshake: 1  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL: TLSv1, cipher:  
“DHE-RSA-AES256-SHA SSLv3 Kx=DH Au=RSA Enc=AES(256) Mac=SHA1”  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_read: -1  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_get\_error: 2  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_read: 1  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_read: 346  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_read: -1  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_get\_error: 2  
2012/02/18 18:12:31 [debug] 723#0: \*1031 event timer del: 12:  
2472717397  
2012/02/18 18:12:31 [debug] 723#0: \*1031 epoll add event: fd:12 op:3  
ev:80000005  
2012/02/18 18:12:31 [debug] 723#0: \*1031 socket 13  
2012/02/18 18:12:31 [debug] 723#0: \*1031 epoll add connection: fd:13  
ev:80000005  
2012/02/18 18:12:31 [debug] 723#0: \*1031 connect to 127.0.0.1:9000,  
fd:13 #1032  
2012/02/18 18:12:31 [debug] 723#0: \*1031 event timer add: 13:  
60000:2472717424  
2012/02/18 18:12:31 [error] 723#0: \*1031 connect() failed (111:  
Connection refused) while connecting to upstream, client: 192.168.1.69,  
server: 192.168.1.68, request: “GET /pcode/register.php HTTP/1.1”,  
upstream: “fastcgi://127.0.0.1:9000”, host: “192.168.1.68”  
2012/02/18 18:12:31 [debug] 723#0: \*1031 event timer del: 13:  
2472717424  
2012/02/18 18:12:31 [debug] 723#0: \*1031 write new buf t:1 f:0 099429A4,  
pos 099429A4, size: 157 file: 0, size: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 write old buf t:1 f:0 099429A4,  
pos 099429A4, size: 157 file: 0, size: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 write new buf t:0 f:0 00000000,  
pos 080E7600, size: 120 file: 0, size: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 write new buf t:0 f:0 00000000,  
pos 080E6460, size: 53 file: 0, size: 0  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL buf copy: 157  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL buf copy: 120  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL buf copy: 53  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL to write: 330  
2012/02/18 18:12:31 [debug] 723#0: \*1031 SSL\_write: 330  
2012/02/18 18:12:31 [debug] 723#0: \*1031 event timer add: 12:  
75000:2472732425

… and nginx.conf

user root;  
worker\_processes 4;

events {

}

http {  
index index.php;  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

server {  
listen 80;

```
location ~* /pcode/(register|loginout).php {
  rewrite ^ https://$host$uri permanent;
}

include /etc/nginx/server_params;

```

}

server {  
listen 443 ssl;  
ssl on;  
ssl\_certificate /usr/local/nginx/conf/server.crt;  
ssl\_certificate\_key /usr/local/nginx/conf/server.key;

```
include /etc/nginx/server_params;

```

}  
}

…the include file server\_params being called:

server\_name 192.168.1.68;  
root /usr/share/nginx/www;

```
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug_event;

location / {
  try_files $uri $uri/ /index.php;
}

location ~ \.php$ {
  include /etc/nginx/fastcgi_params;
  try_files $uri =404;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param HTTPS on;
}

location @rewrites{
  rewrite ^ /index.php last;
}

#catch static file requests
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
  expires max;
  add_header Pragma public;
  add_header Cache-Control "public, must-revalidate,

```

proxy-revalidate";  
}

```
#prevent hidden file requests --starting with a period

location ~ /\. {
  access_log off; log_not_found off; deny all;
}

```

Posted at Nginx Forum:

> **[php exits with 502 Bad Gateway](https://forum.nginx.org/read.php?2,222625,222625#msg-222625)**
>
> Hello I have a problem where php exits with a 502 Bad Gateway error every once in a while. It appears to be unpredictable. When it happens I have to re-start the vm -- restarting nginx doesn't fix it. The OS is Ubuntu running on VirtualBox and I am...

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 5:20pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/2 "2012-02-19T17:20:29Z")

</div>

What is the contents of /etc/nginx/fastcgi\_params

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 5:21pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/3 "2012-02-19T17:21:44Z")

</div>

Also the header (first 20 - 30 lines or so) of /etc/init.d/php-cgi or  
whatever PHP startup script you are using

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 8:05pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/4 "2012-02-19T20:05:15Z")

</div>

Sorry for missing that conf. portion.

…the fastcgi\_params file

fastcgi\_param QUERY\_STRING $query\_string;  
fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param CONTENT\_TYPE $content\_type;  
fastcgi\_param CONTENT\_LENGTH $content\_length;

fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param DOCUMENT\_URI $document\_uri;  
fastcgi\_param DOCUMENT\_ROOT $document\_root;  
fastcgi\_param SERVER\_PROTOCOL $server\_protocol;

fastcgi\_param GATEWAY\_INTERFACE CGI/1.1;  
fastcgi\_param SERVER\_SOFTWARE nginx;

fastcgi\_param REMOTE\_ADDR $remote\_addr;  
fastcgi\_param REMOTE\_PORT $remote\_port;  
fastcgi\_param SERVER\_ADDR $server\_addr;  
fastcgi\_param SERVER\_PORT $server\_port;  
fastcgi\_param SERVER\_NAME $server\_name;

#fastcgi\_param HTTPS $server\_https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi\_param REDIRECT\_STATUS 200;

… and the entire php-fcgi file

#!/bin/bash  
BIND=127.0.0.1:9000  
USER=www-data  
PHP\_FCGI\_CHILDREN=15  
PHP\_FCGI\_MAX\_REQUESTS=1000

PHP\_CGI=/usr/bin/php-cgi  
PHP\_CGI\_NAME=`basename $PHP_CGI`  
PHP\_CGI\_ARGS=“- USR=$USER PATH=/usr/bin  
PHP\_FCGI\_CHILDREN=$PHP\_FCGI\_CHILDREN  
PHP\_FCGI\_MAX\_REQUESTS=$PHP\_FCGI\_MAX\_REQUESTS $PHP\_CGI -b $BIND”  
RETVAL=0  
PHP\_CONFIG\_FILE=/etc/php5/cgi/php.ini

start() {  
echo -n "Starting PHP FastCGI: "  
start-stop-daemon --quiet – start --background --chuid “$USER” --exec  
/usr/bin/env – $PHP\_CGI\_ARGS  
RETVAL=$?  
echo “$PHP\_CGI\_NAME.”  
}  
stop() {  
echo -n "Stopping PHP FastCGI: "  
killall -q -w -u $USER $PHP\_CGI  
RETVAL=$?  
echo “$PHP\_CGI\_NAME.”  
}

case “$1” in  
start)  
start  
;;  
stop)  
stop  
;;  
restart)  
stop  
start  
;;  
\*)  
echo “Usage: php-fastcgi {start|stop|restart}”  
exit 1  
;;  
esac  
exit $RETVAL

Thanks for your help!  
Fouwaaz

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,222625,222675#msg-222675](http://forum.nginx.org/read.php?2,222625,222675#msg-222675)

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 8:19pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/5 "2012-02-19T20:19:32Z")

</div>

Hi

I am not very conversant with this. I didn’t do any compilation of my  
own just downloaded the .deb package. The nginx.conf, though, I created  
new.

Thanks!

Posted at Nginx Forum:

> **[Re: php exits with 502 Bad Gateway](https://forum.nginx.org/read.php?2,222625,222678#msg-222678)**
>
> Hello I have a problem where php exits with a 502 Bad Gateway error every once in a while. It appears to be unpredictable. When it happens I have to re-start the vm -- restarting nginx doesn't fix it. The OS is Ubuntu running on VirtualBox and I am...

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 8:10pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/6 "2012-02-19T20:10:51Z")

</div>

Are these distribution packages (.deb / .rpm) or compiled from source?

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 8:48pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/7 "2012-02-19T20:48:02Z")

</div>

I’ve had this problem in the past and I replaced /etc/init.d/php-cgi  
with one that enables FastCGI and that stopped the problems. I think  
it’s Ubuntu specific because I cannot recreate the problem (on a  
virtual server) on Debian.

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 19, 2012, 9:38pm UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/8 "2012-02-19T21:38:20Z")

</div>

Hi there

Thanks for the reply and I am trying to do that. But how? Do I  
download php again and re-install it? Where can I find the replacement  
file? If you can walk me through the process, it will be greatly  
appreciated.

Posted at Nginx Forum:

> **[Re: php exits with 502 Bad Gateway](https://forum.nginx.org/read.php?2,222625,222684#msg-222684)**
>
> Hello I have a problem where php exits with a 502 Bad Gateway error every once in a while. It appears to be unpredictable. When it happens I have to re-start the vm -- restarting nginx doesn't fix it. The OS is Ubuntu running on VirtualBox and I am...

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 20, 2012, 2:32am UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/9 "2012-02-20T02:32:36Z")

</div>

Hello

On Mon, Feb 20, 2012 at 3:37 AM, mfouwaaz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply and I am trying to do that. But how? Do I  
> download php again and re-install it? Where can I find the replacement  
> file? If you can walk me through the process, it will be greatly  
> appreciated.

Seems like your php-cgi got closed at one point which is why  
restarting nginx didn’t help.

Try running php-cgi with php-fpm/supervisord/god/monit. There are lots  
of tutorial on web for it.

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 22, 2012, 7:33am UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/10 "2012-02-22T07:33:18Z")

</div>

If you feel like playing with reliability, use CentminMod. It requires  
a clean install of CentOS, run the installer and it installs nginx,  
MariaDB (100% compatible MySQL database), php-fpm and other goodies. I  
recommend it. You just choose option 1 for the initial setup and  
option 2 for the domain.

PS works really great on vBulletin

---

<div class="post-metadata">

**Author:** ![mfouwaaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mfouwaaz](https://www.ruby-forum.com/u/mfouwaaz)\
**Post date:** [February 22, 2012, 5:24am UTC](https://www.ruby-forum.com/t/php-exits-with-502-bad-gateway/217002/11 "2012-02-22T05:24:13Z")

</div>

Hello fbhosted and Edho A.

I enabled php-fpm and things are looking ok for now. I hope it lasts.

Thanks again for your help!  
Fouwaaz

Posted at Nginx Forum:

> **[Re: php exits with 502 Bad Gateway](https://forum.nginx.org/read.php?2,222625,222826#msg-222826)**
>
> Hello I have a problem where php exits with a 502 Bad Gateway error every once in a while. It appears to be unpredictable. When it happens I have to re-start the vm -- restarting nginx doesn't fix it. The OS is Ubuntu running on VirtualBox and I am...
