Closed
Description
The Native HTTP Support does not support HTTP Keep-Alive. It's easy to test with uwsgi --http-socket :8080
and using telnet
to send a valid HTTP/1.1 request; uwsgi will close the socket after sending the response.
A well-behaved HTTP/1.1 server will keep the socket open unless the client requests Connection: Close
.
Activity
Darvame commentedon Nov 2, 2015
uwsgi --http-socket :8080 --http-keepalive --add-header "Connection: keep-alive"
parasyte commentedon Nov 2, 2015
@Darvame Perhaps you could try this yourself, because it does not work:
Run the server:
Test keepalive:
The following invocation works as expected:
Test keepalive:
Here are a few backtraces from
lldb
(plain ol' Python and gevent plugin) showing where the close is occurring unconditionally:Python: https://github.com/unbit/uwsgi/blob/2.0.11.2/core/loop.c#L149
gevent: https://github.com/unbit/uwsgi/blob/2.0.11.2/plugins/gevent/gevent.c#L325
These lines also execute with
--http :8080
(i.e. in the working case), but I assume it's closing the socket on the backend?Darvame commentedon Nov 2, 2015
Yes, does not work for me too :C
unbit commentedon Nov 2, 2015
You need uWSGI 2.1 (master branch) and the --http11-socket option. Once set the server will try to maintain the connection opened if a bunch of rules are respected. This is not a smart http 1.1 parser (to avoid parsing the whole response) but assumes the developer is generating the right headers. It has been added to support RTSP protocol for video streaming.
depaolim commentedon Nov 6, 2015
Does this new option replace completely the old ones?
I used
and I obtained this:
without any "Connection closed by foreign host."
So it seems to work as a "Keep-alive" without any need of the old options
Does it mean that they are no more necessary?
xrmx commentedon Nov 13, 2015
@depaolim i think --http11-socket may replace the add-header and http-keepalive options but i don't see it touching tcp stuff as so-keepalive does.
http: document master's keep-alive related option http11-socket
xrmx commentedon Nov 15, 2015
Documentation has been updated thanks to @depaolim, closing this.
marc1n commentedon Apr 1, 2017
I confirm (by testing it) that "http-keepalive" option works only with "http" (not "http-socket") in uWSGI 2.0.
Why it is "http-keepalive" not working with "http-socket"? Is it any reason behind this?
The "http11-socket" is a future option (in uWSGI 2.1 which for now is not released yet) so in reality I cannot use HTTP Keep-Alive in uWSGI not paying penalty of "http" option overhead.
Btw, "http-keepalive" option value is a number which specify timeout (in seconds) after which an inactive connection is closed by server. How can I configure this timeout when I will use "http11-socket"?
ushuz commentedon Apr 30, 2018
Seconding @marc1n, we observed the same problem.
http-keepalive
doesn't work withhttp-socket
but work withhttp
. There's also a stackoverflow question reporting the same.http11-socket
does enable keepalive, but it doesn't work well withgevent
.UPDATE (14 Nov 2018):
http11-socket
doesn't work well with non-gevent
either, half workers got jammed after a while.jf commentedon Sep 15, 2020
@marc1n what is your source for this? I see #2018, which seems to indicate that this is a boolean instead.
marc1n commentedon Sep 15, 2020
@jf From the source code: https://github.com/unbit/uwsgi/blob/uwsgi-2.0/plugins/http/http.c#L658
jf commentedon Sep 15, 2020
Excellent, thanks! I see it now. On another note, if anybody has any recommendations for another server to check out that has proper http keep-alive support, I'd be glad to hear it.
xqliang commentedon Nov 22, 2022
--http-socket
+--http-keepalive
+add-header
not worked, and--http11-socket
works like--http
+--http-keepalive
+add-header
,but theadd-header
is still needed for some clients(e.g. ab -k) to keep connection open(alive). BTW, use--socket-timeout
(defaults to 4s) to set the keepalive timeout in seconds.NOTE: Currently(uWSGI==2.0.21 2022.10.25) only
--http
+--http-keepalive
+add-header
works well with Nginx keepalive, #2133.