keepalive connection to fastcgi backend hangs

Maxim Dounin mdounin at
Mon Dec 20 14:11:49 UTC 2021


On Sun, Dec 19, 2021 at 07:56:51PM +0000, Nicolas Franck wrote:

> In order to make nginx keep the tcp connections alive,
> I've added the following settings:
> * proxy_socket_keepalive on
> * proxy_http_version 1.1;
> * proxy_set_header Connection "";

Just a side note: you don't need any of these to keep FastCGI 
connections alive.

> * fastcgi_keep_conn on;
> * added an upstream "fgi":
> upstream fcgi {
>     keepalive 10;
>     server myhost:9000;
> }
> * added a location block (only snippet given):
> location /fcgi {
>    fastcgi_pass_request_headers on;
>    fastcgi_pass fcgi;
>    fastcgi_keep_conn on;
> }
> What I see: after a couple of requests nginx "hangs" when I 
> visit path "/fcgi".
> This disappears when
> * I remove the setting "keepalive" from the upstream (but that 
> disables keepalive altogether)
> * bind the fcgi application to a unix socket, and let nginx bind 
> to that. But that requires nginx and the fcgi to be on the same 
> server.
> * reduce the number of nginx workers to exactly 1. Not sure why 
> that works.
> * I spawn the application with tool "supervisord" (a fcgi 
> process manager written in python)
> Does anyone know what is happening here?
> Fcgi has little documentation on the web..


> Spawn: spawn-fcgi -f /path/to/fcgi_example.cpp -p 9000

The spawn-fcgi defaults to 1 child process, and each child process 
can handle just one connection.  On the other hand, your 
configuration instruct nginx to cache up to 10 connections per 
nginx process.

As long as the only one connection your upstream server can handle 
is cached in another nginx process, nginx won't be able able to 
reach the upstream server, and will return 504 Gateway Timeout 
error once the fastcgi_connect_timeout expires (60s by default).  
Likely this is something you see as "hangs".

Obvious fix would be to add additional fastcgi processes.  Given 
"keepalive 10;" in nginx configuration, you'll need at least 10 * 
<number of nginx worker processes>.  Something like:

spawn-fcgi -F 20 -f /path/to/fcgi -p 9000

should fix things for up to 2 nginx worker processes.

Just in case, that's exactly the problem upstream keepalive 
documentation warns about ( "The 
connections parameter should be set to a number small enough to 
let upstream servers process new incoming connections as well".

Maxim Dounin

More information about the nginx mailing list