<div dir="ltr">You generally want as long a keepalive timeout as you're able to tolerate. Closing the connection after 5 seconds for example means a full new TCP and TLS handshake has to occur if the user clicks a link after 5 seconds, resulting in a minimum of two RTTs before content, causing a slow loading experience. If you have long pages that could take minutes to read before a user loads another page, then for best performance you want keepalive at 5+ minutes for example. Keep in mind this is an upper limit - the browser is free to choose when to close the connection on the client too.<div><br></div><div>The reason you will sometimes see recommendations to have a low keepalive timeout is to improve performance of legacy forking web servers where each client connection consumed an entire process and thus lots of memory. Having lots of idle clients on such servers caused heavy memory usage and was not recommended. Of course nginx has no such problem since it's event based.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 17, 2016 at 10:49 AM, Aahan Krish <span dir="ltr"><<a href="mailto:krish@aahan.me" target="_blank">krish@aahan.me</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'd like to understand how `keepalive_timeout` works. The description<br>
in the documentation isn't very clear and I couldn't get an answer to<br>
my satisfaction on #nginx IRC.<br>
<br>
People seem to have their own understanding of how `keepalive_timeout`<br>
works and they are very different.<br>
<br>
1. For e.g. <<a href="https://github.com/roots/trellis/blob/master/roles/nginx/templates/nginx.conf.j2" rel="noreferrer" target="_blank">https://github.com/roots/trellis/blob/master/roles/nginx/templates/nginx.conf.j2</a>><br>
describes the directive like this:<br>
<br>
    # How long to allow each connection to stay idle; longer values are better<br>
    # for each individual client, particularly for SSL, but means that worker<br>
    # connections are tied up longer. (Default: 65)<br>
<br>
So according to the statement `keepalive_timeout` is to be used to<br>
tell how long a server needs to keep the connection alive AFTER<br>
serving all the requests.<br>
<br>
And this commenter seems to agree:<br>
<<a href="http://serverfault.com/q/331762#comment665957_331764" rel="noreferrer" target="_blank">http://serverfault.com/q/331762#comment665957_331764</a>><br>
<br>
> [It's] only a timeout during which server waits for another request<br>
> up to [the limit set by] `keepalive_requests`. So it is not important<br>
> how much time it takes for the full page to load [...] BTW the max.<br>
> time connection is kept open is almost:<br>
><br>
>     keepalive_timeout * keepalive_requests<br>
<br>
2. Then there are people understand the function of<br>
`keepalive_timeout` like so: <a href="http://stackoverflow.com/a/10202959" rel="noreferrer" target="_blank">http://stackoverflow.com/a/10202959</a><br>
<br>
> HTTP Keep-Alive is a feature of HTTP protocol. The web-server,<br>
> implementing Keep-Alive Feature, has to check the connection/socket<br>
> periodically (for incoming HTTP request) for the time span since it<br>
> sent the last HTTP response (in case there was corresponding HTTP<br>
> Request). If no HTTP request is received by the time of the configured<br>
> keep-alive time (seconds) the web server closes the connection.<br>
<br>
Similar opinion from another: <a href="http://serverfault.com/a/331764" rel="noreferrer" target="_blank">http://serverfault.com/a/331764</a><br>
<br>
> There isn't a good one-size-fits-all answer; if most client browsers<br>
> are able to load all the resources on a page in 5 seconds then 5<br>
> seconds is just fine [...]<br>
<br>
So according to these people the directive is to be calculated in such<br>
a way that in that given time (on an average) a web page on your site<br>
and its contents (HTML, CSS, JS, images) are completely loaded on most<br>
clients.<br>
<br>
THE QUESTION: So which one is it? (1) or (2)? Who's right?<br>
<br>
_______________________________________________<br>
nginx mailing list<br>
<a href="mailto:nginx@nginx.org">nginx@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx" rel="noreferrer" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx</a><br>
</blockquote></div><br></div>