nginx 502 bad gateway - fastcgi not listening? (Debian 5)
On my server I use nginx+fcgi as well.
My solution isn't foolproof but at least works. I have this script which uses spawn-fcgi and php5-cgi under /etc/init.d/
#!/bin/bash
PHP_SCRIPT='/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi'
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
and the related nginx conf is this:
server {
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME /var/www/hyperblasted/$fastcgi_script_name;
}
...
}
and the fastcgi.conf contains the following
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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 REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
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;
Hope this helps :)
PS: With this setup I had an issue where the cgi daemon would die every now and then. I worked around this issue by executing this in a cronjob every 5 minutes:
if ps aux | grep 'php5-cgi' | grep -v grep > /dev/null ; then
echo "PHP-cgi is runnning !"
else
echo "PHP-cgi is down. Starting over..."
/etc/init.d/php-fcgi start
fi
Sean
Updated on September 17, 2022Comments
-
Sean over 1 year
I have experience with nginx but it's always been pre-installed for me (via VPS.net pre-configured image). I really like what it does for me, and now I'm trying to install it on my own server with apt-get. This is a fairly fresh Debian 5 install. I have few extra packages installed but they're all .deb's, no manual compiling or anything crazy going on.
Apache is already installed but I disabled it. I did apt-get install nginx and that worked fine. Changed the config around a bit for my needs, although the same problem I'm about to describe happens even with the default config.
It took me a while to figure out that the default debian package for nginx doesn't spawn fastcgi processes automatically. That's pretty lame, but I figured out how to do that with this script, which I found posted on many different web sites:
#!/bin/bash ## ABSOLUTE path to the PHP binary PHPFCGI="/usr/bin/php5-cgi" ## tcp-port to bind on FCGIPORT="9000" ## IP to bind on FCGIADDR="127.0.0.1" ## number of PHP children to spawn PHP_FCGI_CHILDREN=10 ## number of request before php-process will be restarted PHP_FCGI_MAX_REQUESTS=1000 # allowed environment variables sperated by spaces ALLOWED_ENV="ORACLE_HOME PATH USER" ## if this script is run as root switch to the following user USERID=www-data ################## no config below this line if test x$PHP_FCGI_CHILDREN = x; then PHP_FCGI_CHILDREN=5 fi ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN" ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS" ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS" if test x$UID = x0; then EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID" else EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT" fi echo $EX # copy the allowed environment variables E= for i in $ALLOWED_ENV; do E="$E $i=${!i}" done # clean environment and set up a new one nohup env - $E sh -c "$EX" &> /dev/null &
When I do a "ps -A | grep php5-cgi", I see the 10 processes running, that should be ready to listen.
But when I try to view a web page via nginx, I just get a 502 bad gateway error.
After futzing around a bit, I tried telneting to 127.0.0.1 9000 (fastcgi is listening on port 9000, and nginx is configured to talk to that port), but it just immediately closes the connection.
This makes me think the problem is with fastcgi, but I'm not sure what I can do to test it. It may just be closing the connection because it's not getting fed any data to process, but it closes immediately so that makes me think otherwise.
So... any advice? I can't figure it out. It doesn't help that it's 1AM, but I'm going crazy here!
-
Admin about 14 yearsdoes
netcat -nap | grep LISTEN | grep 9000
show the listening socket? w/ php-fcgi php handles the error handling, but when my home-grown fcgi scripts fail to interact w/ fcgi correctly, you can get a 502 error as well. -
Admin about 14 yearsWhen I do that it says: all-A-records NIY
-
Admin about 14 yearsYou might be interested in using php-fpm sapi. I just had the very symptoms and it turned out that my memcached extension was incompatible with libmemcached 0.39. Only place this showed was in the php-fpm logs!
-
-
Sean about 14 yearsThat's the thing though, when I installed nginx with apt-get, there is no spawn-fcgi binary file. That's why I had to figure out on my own how to spawn the PHP CGI listeners, And they are running... they're just not processing anything.
-
Eduardo Ivanec almost 13 years
apt-get install spawn-fcgi
. It works on sid. -
Adrian Heine over 11 yearsDo you mean the actual owner of the file or the value of the variable
USERID
as set in the file?