FastCGI and PHP

Grzegorz Nosek grzegorz.nosek at gmail.com
Wed Apr 2 14:32:48 MSD 2008


On Wed, Apr 02, 2008 at 08:08:57AM +0100, Phillip B Oldham wrote:
> After the recent thread on php-fastcgi and memory leaks, I've realised 
> that I'm a little unsure as to exactly how nginx and php-fastcgi 
> communicate with one another. I'm wondering whether anyone could spare 
> the time for clarification?

You have already received a few good responses, but let me throw in my
0.02PLN too.

> I understand that when started (in my case using spawn-fastcgi from the 
> lighttpd project) php-fastcgi creates a master process and a number of 
> child processes. Nginx then passes requests through to php-fastcgi, 
> which processes the request and returns a response.

Yup.

> What I'm unsure on is whether nginx is passing the request directly to 
> one of the child processes or the master process which then delegates.

Selection of the child process is done implicitly by the kernel. All the
children are sleeping in accept() on the same socket, so there's no
delegation process to speak of.

This approach has both advantages (mostly trivial implementation)
and disadvantages, mostly that the web server is left in the dark about
overloaded fcgi instances, their activity etc. It also prevents
knowledge of e.g. whether there are any ready backends at all -- this
is a pain when running under a process manager like mod_fastcgi for
apache.

> I'm also unsure as to how nginx passes through the fastcgi params we 
> configure.

I'm sure you'll find the protocol details in the spec, I just know it's
a binary protocol.

> The reason I ask is that I have some useful components written in Eiffel 
> which I'd like to make available via a webserver. I've found a small 
> fastcgi server written in Eiffel, which I'd like to expand on to 
> replicate the kind of through-put the php-fastcgi instance I'm running 
> allows.

I'm not sure whether the fastcgi protocol supports multiplexing on a
single socket and the fcgi library insists on emulating stdio, so in the
worst case you'll have to use a prefork/postfork/threaded/etc. model.

I don't know how well libfcgi copes with multiplexed I/O (e.g. will
FCGI_getc always return immediately after a select returns) and "almost
a socket" abstractions tend to have problems with that so beware.

Good luck and please share your findings :)

Best regards,
 Grzegorz Nosek





More information about the nginx mailing list