keepalive connection to fastcgi backend hangs

Nicolas Franck Nicolas.Franck at
Sun Dec 19 19:56:51 UTC 2021

I've created a server setup where nginx acts as
a proxy server for a fastcgi application.
That last application is running on a different server on port 9000.
It is spawn with spawn-fcgi.
Recently I have found out that nginx
closes the connection after every request.

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 "";
* 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..

Example of an application: fcgi_example.cpp

#include <iostream>
#include <fcgio.h>
#include <fcgiapp.h>

void handle_request(FCGX_Request& request){
    fcgi_streambuf cout_fcgi_streambuf(request.out);
    std::ostream os{&cout_fcgi_streambuf};

    os << "HTTP/1.1 200 OK\r\n"
       << "Content-type: text/plain\r\n\r\n"
       << "Hello!\r\n";

int main(){
    FCGX_Request request;
    FCGX_InitRequest(&request, 0, 0);
    while (FCGX_Accept_r(&request) == 0) {

Build: g++ -std=c++11 -lfcgi -lfcgi++ -o fcgi_example fcgi_example.cpp

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list