Php exits with 502 Bad Gateway

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:

What is the contents of /etc/nginx/fastcgi_params

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

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:

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:

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

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.

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:

Hello

On Mon, Feb 20, 2012 at 3:37 AM, mfouwaaz [email protected] 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

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

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: