Stability of fastcgi on nginx

Hi Linoders,

I'm a relative newbie to this VPS world, and jumped in with nginx 07.62 for my wordpress blog.

Unfortunately the fastcgi process is unstable, I find that it goes down every other day. The symptom's of such a crash are nginx 502 errors, both when I attempt to load the /wp-admin page, and on redirect after leaving a blog comment.

To fix the fault, it requires me to reboot or run a
> /etc/init.d/init-php restart .

From this afternoon's crash, Nginx's error.log shows the following
> 2010/01/06 14:14:23 [error] 2373#0: *30147 connect() failed (111: Connection refused) while connecting to upstream, client: 174.37.205.87, server: www.mydomain.com, request: "GET /feed/atom/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:14:47 [error] 2373#0: *30149 connect() failed (111: Connection refused) while connecting to upstream, client: 87.250.254.241, server: www.mydomain.com, request: "GET /forum/index.php?topic=9413.msg49196 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:14:55 [error] 2373#0: *30151 connect() failed (111: Connection refused) while connecting to upstream, client: 134.159.131.34, server: www.mydomain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:15:04 [error] 2373#0: *30153 connect() failed (111: Connection refused) while connecting to upstream, client: 66.249.68.146, server: www.mydomain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:15:04 [error] 2373#0: *30156 connect() failed (111: Connection refused) while connecting to upstream, client: 220.253.177.77, server: www.mydomain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:15:11 [error] 2373#0: *30159 connect() failed (111: Connection refused) while connecting to upstream, client: 209.85.238.16, server: www.mydomain.com, request: "GET /feeds/posts/default HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:15:14 [error] 2373#0: *30161 connect() failed (111: Connection refused) while connecting to upstream, client: 134.159.131.34, server: www.mydomain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:16:18 [error] 2619#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 134.159.131.34, server: www.mydomain.com, request: "GET /forum/index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:16:19 [error] 2622#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 216.18.20.8, server: www.mydomain.com, request: "GET /feed/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com"

2010/01/06 14:17:45 [error] 2380#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 218.185.92.146, server: www.mydomain.com, request: "GET /the-start-of-the-melbourne-rebellion/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mydomain.com", referrer: "http://www.google.com/search?q=Harold+M … lz=1I7ADBR">http://www.google.com/search?q=Harold+MItchell+Melbourne+rebels&rls=com.microsoft:en-au:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7ADBR"

I've tried a few different approaches to run fastcgi, namely libfcgi0

, spawn-cgi, /usr/bin/php-cgi -b 127.0.0.1:9000, and now I'm considering PHP-FPM.

Does this sound like a reasonable strategy? I'm at the point where I might give up on nginx and just install apache2! I'd really rather not though, nginx absolutely flies when it's running..

I'd really appreciate any advice on what to check, or any alternatives I haven't yet found.

Cheers,

Moses.

7 Replies

Hi,

i was in the same boat as you a few month ago, and switch to nginx proxy to apache, instead of fastcgi.

You just need to slow down apache and turn off keep alive:

StartServers 3

MinSpareServers 2

MaxSpareServers 2

MaxClients 25

MaxRequestsPerChild 1000

For me this is the most stable and functional configuration I can have without loosing htaccess (I'm lazy to switch to nginx rules).

I only proxy php requests to apache. All other non script files (txt,, gif, png, etc) are served directly by nginx.

Where did you get that init-php script? What does it do?

Does your script set the PHPFCGIMAX_REQUESTS environment variable? Some versions of PHP will hang after a certain number of requests are served, so the environment variable above tells it to restart automatically before that happens. Try a value of 500 or less.

Examples:

http://redmine.lighttpd.net/projects/li … iptsUbuntu">http://redmine.lighttpd.net/projects/lighttpd/wiki/ScriptsUbuntu

(This is for spawn-fcgi)

http://drupal.org/node/110224

(Without spawn-fcgi)

I personally prefer spawn-fcgi. Newer versions of it can even spawn multiple processes, each with their own children, listening on the same port. If one process hangs, others will keep working. Try that too. But you can't use an opcode cacher (APC, XCache) with multiple processes unless you want to waste a bit of RAM.

Resorting to Apache is unnecessary. It's not impossible to get stable fastcgi with nginx, and I've had no such troubles with Lighttpd either.

@Moses:

spawn-cgi

spawn-cgi isn't perfect. I've heard many people complaining about it. PHP-FPM is any day better than spawn-cgi. If you're in the dark, follow this link: http://php-fpm.org/ :)

Could it be a config issue with the way I'm currently set up? Here's my config files in case there's anything obvious..

/usr/local/nginx/sites-available/default
> server {

listen 80;

server_name localhost;

location / {

root html;

index index.php index.html index.htm;

}

redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html;

location = /50x.html

{

root html;

}

}

/usr/local/nginx/sites-enabled/mydomain.com
> server {

listen 80;

server_name mydomain.com;

rewrite ^/(.*) http://www.mydomain.com/$1 permanent;

}

server {

listen 80;

server_name www.mydomain.com;

accesslog /home/publichtml/mydomain.com/log/access.log;

errorlog /home/publichtml/mydomain.com/log/error.log;

location / {

root /home/public_html/mydomain.com/public/;

index index.php index.html;

Wordpress WP Super Cache plugin and permalinks.

include /usr/local/nginx/conf/wordpressparams.supercache;

}

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .php$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include /usr/local/nginx/conf/fastcgi_params;

fastcgiparam SCRIPTFILENAME /home/publichtml/mydomain.com/public/$fastcgiscript_name;

}

location ^~ /munin {

root /home/public_html/mydomain.com/public;

auth_basic "Restricted";

authbasicuser_file /usr/local/nginx/.htpasswd;

}

}

/etc/init.d/php-fastcgi-new
> #!/bin/bash

BIND=127.0.0.1:9000

USER=www-data

PHPFCGICHILDREN=5

PHPFCGIMAX_REQUESTS=500

PHP_CGI=/usr/bin/php-cgi

PHPCGINAME=basename $PHP_CGI

PHPCGIARGS="- USER=$USER PATH=/usr/bin PHPFCGICHILDREN=$PHPFCGICHILDREN PHPFCGIMAXREQUESTS=$PHPFCGIMAXREQUESTS $PHP_CGI -b $BIND"

RETVAL=0

start() {

echo -n "Starting PHP FastCGI: "

start-stop-daemon –quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHPCGIARGS

RETVAL=$?

echo "$PHPCGINAME."

}

stop() {

echo -n "Stopping PHP FastCGI: "

killall -q -w -u $USER $PHP_CGI

RETVAL=$?

echo "$PHPCGINAME."

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo "Usage: php-fastcgi {start|stop|restart}"

exit 1

;;

esac

/usr/local/nginx/conf/fastcgiparams > fastcgiparam QUERYSTRING $querystring;

fastcgiparam REQUESTMETHOD $request_method;

fastcgiparam CONTENTTYPE $content_type;

fastcgiparam CONTENTLENGTH $content_length;

fastcgiparam SCRIPTNAME $fastcgiscriptname;

fastcgiparam REQUESTURI $request_uri;

fastcgiparam DOCUMENTURI $document_uri;

fastcgiparam DOCUMENTROOT $document_root;

fastcgiparam SERVERPROTOCOL $server_protocol;

fastcgiparam GATEWAYINTERFACE CGI/1.1;

fastcgiparam SERVERSOFTWARE nginx/$nginx_version;

fastcgiparam REMOTEADDR $remote_addr;

fastcgiparam REMOTEPORT $remote_port;

fastcgiparam SERVERADDR $server_addr;

fastcgiparam SERVERPORT $server_port;

fastcgiparam SERVERNAME $server_name;

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

fastcgiparam REDIRECTSTATUS 200;

/usr/local/nginx/conf/nginx.conf
> user www-data www-data;

worker_processes 4;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

tcp_nopush on;

tcp_nodelay off;

keepalive_timeout 5;

gzip on;

gzipcomplevel 2;

gzip_proxied any;

gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

include /usr/local/nginx/sites-enabled/*;

}

Don't see anything obvious, other than the fact that

fastcgi_param SCRIPT_FILENAME /home/public_html/mydomain.com/public/$fastcgi_script_name;

could be

fastcgi_param SCRIPT_FILENAME /home/public_html/mydomain.com/public$fastcgi_script_name;

($fastcgiscriptname already comes with a leading slash)

But that shouldn't be causing the problem.

A few suggestions (not solutions):

Do you have logging enabled in your php.ini? Is there a particular request or error that always occurs just before PHP goes belly up? That could help track down the problem.

Did you install PHP from your distro's repository (apt, yum) or did you compile it yourself? Try reinstalling, e.g. apt-get install –reinstall php5-common php5-cgi

Does your FastCGI process just die, or does it stay alive but hang?

While you're troubleshooting this, here's a band-aid: write up a quick script that runs from crontab every minute, which checks if PHP is alive, and restarts PHP if not. If PHP just dies, ps -C php5-cgi -o cmd= should be enough to tell the difference. (No result if PHP is dead.) I used to do something like this back when lighttpd used to have hideous memory leaks 8)

Moses,

Love your site. I've been following it ever since you posted about nginx/wordpress in December. Wordpress and your forum is always up and fast here in Texas.

Are you having problems keeping nginx going? I ran in to similar issues but worked them out last summer. Let me know if you are and I'll look up what I did. (I have to make notes, less things to permanently forget)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct