<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
I've created a server setup where nginx acts as<br class="">
a proxy server for a fastcgi application.<br class="">
That last application is running on a different server on port 9000.
<div class="">It is spawn with <b class="">spawn-fcgi</b>.<br class="">
Recently I have found out that nginx<br class="">
closes the connection after every request.<br class="">
<br class="">
In order to make nginx keep the tcp connections alive,<br class="">
I've added the following settings:<br class="">
<br class="">
* <b class="">proxy_socket_keepalive on</b><br class="">
* <b class="">proxy_http_version 1.1;<br class="">
</b>* <b class="">proxy_set_header Connection "";</b><br class="">
* <b class="">fastcgi_keep_conn on;</b>
<div class="">* added an upstream "fgi":</div>
<div class=""><br class="">
</div>
<div class="">upstream fcgi {</div>
<div class=""> keepalive 10;</div>
<div class=""> server myhost:9000;</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class="">* added a location block (only snippet given):</div>
<div class=""><br class="">
</div>
<div class="">location /fcgi {</div>
<span class=""> fastcgi_pass_request_headers on;<br class="">
</span><span class=""> fastcgi_pass fcgi;</span>
<div class=""><span class=""> fastcgi_keep_conn on;</span>
<div class="">}
<div class=""><br class="">
</div>
<div class="">What I see: after a couple of requests nginx "hangs" when I visit path "/fcgi".</div>
<div class=""><br class="">
</div>
<div class="">This disappears when</div>
<div class=""><br class="">
</div>
<div class="">* I remove the setting "keepalive" from the upstream (but that disables keepalive altogether)</div>
<div class="">* 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.</div>
<div class="">* reduce the number of nginx workers to exactly 1. Not sure why that works.</div>
<div class="">* I spawn the application with tool "supervisord" (a fcgi process manager written in python)</div>
<div class=""><br class="">
</div>
<div class="">Does anyone know what is happening here?</div>
<div class="">Fcgi has little documentation on the web..</div>
<div class=""><br class="">
</div>
<div class="">Example of an application: fcgi_example.cpp</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #c814c9" class="">#include
</span><span style="font-variant-ligatures: no-common-ligatures" class=""><iostream></span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(200, 20, 201);" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">#include </span>
<span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class=""><fcgio.h></span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #c814c9" class="">#include
</span><span style="font-variant-ligatures: no-common-ligatures" class=""><fcgiapp.h></span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d" class="">void</span><span style="font-variant-ligatures: no-common-ligatures" class=""> handle_request(FCGX_Request& request){</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> fcgi_streambuf cout_fcgi_streambuf(request.out);</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> std::ostream os{&cout_fcgi_streambuf};</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> os <<
</span><span style="font-variant-ligatures: no-common-ligatures" class="">"HTTP/1.1 200 OK</span><span style="font-variant-ligatures: no-common-ligatures; color: #c814c9" class="">\r\n</span><span style="font-variant-ligatures: no-common-ligatures" class="">"</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> <<
</span><span style="font-variant-ligatures: no-common-ligatures" class="">"Content-type: text/plain</span><span style="font-variant-ligatures: no-common-ligatures; color: #c814c9" class="">\r\n\r\n</span><span style="font-variant-ligatures: no-common-ligatures" class="">"</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> << </span>
<span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">"Hello!</span><span style="font-variant-ligatures: no-common-ligatures; color: #c814c9" class="">\r\n</span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">"</span><span style="font-variant-ligatures: no-common-ligatures" class="">;</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d" class="">int</span><span style="font-variant-ligatures: no-common-ligatures" class=""> main(){</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> FCGX_Request request;</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> FCGX_Init();</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; background-color: #e0e409" class="">FCGX_InitRequest</span><span style="font-variant-ligatures: no-common-ligatures" class="">(&request,
</span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">,
</span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">);</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">while</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (FCGX_Accept_r(&request)
== </span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> handle_request(request);</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div>
</div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><br class="">
</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">Build: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">g++
</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 20, 201);" class="">-std</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(193, 101, 28);" class="">=</span><span style="font-variant-ligatures: no-common-ligatures;" class="">c++</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 36, 25);" class="">11</span><span style="font-variant-ligatures: no-common-ligatures;" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 20, 201);" class="">-lfcgi</span><span style="font-variant-ligatures: no-common-ligatures;" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 20, 201);" class="">-lfcgi</span><span style="font-variant-ligatures: no-common-ligatures;" class="">++
</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 20, 201);" class="">-o</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> fcgi_example fcgi_example.cpp</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class=""><br class="">
</span></div>
<div style="margin: 0px; font-stretch: normal; font-size: 18px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">Spawn: spawn-fcgi -f /path/to/fcgi_example.cpp -p 9000 </span></div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<br class="">
<br class="">
</div>
</div>
</div>
</div>
</body>
</html>