Default value of nginx keepalive

2020-07-13 08:59发布

From nginx.org, the default value of keepalive config is —, however I don't quite understand what does this mean.

Syntax: keepalive connections;
Default:    —
Context:    upstream
This directive appeared in version 1.1.4.

标签: nginx
1条回答
爷、活的狠高调
2楼-- · 2020-07-13 09:35

In order Nginx to keep TCP connection alive both upstream section and origin server should be configured to not finalise the connection. Upstream section keepalive default value means no keepalive, hence connection won't be reused, each time you can see TCP stream number increases per every request to origin server, opposite to what happens with keepalive. You can check it with using tcpdump.

10 Tips for 10x Application Performance blog post describes it very well:

Client keepalives – Keepalive connections reduce overhead, especially when SSL/TLS is in use. For NGINX, you can increase the maximum number of keepalive_requests a client can make over a given connection from the default of 100, and you can increase the keepalive_timeout to allow the keepalive connection to stay open longer, resulting in faster subsequent requests.

Upstream keepalives – Upstream connections – connections to application servers, database servers, and so on – benefit from keepalive connections as well. For upstream connections, you can increase keepalive, the number of idle keepalive connections that remain open for each worker process. This allows for increased connection reuse, cutting down on the need to open brand new connections. For more information, refer to our blog post, HTTP Keepalive Connections and Web Performance.

See also RFC-793 Section 3.5:

A TCP connection may terminate in two ways: (1) the normal TCP close sequence using a FIN handshake, and (2) an "abort" in which one or more RST segments are sent and the connection state is immediately discarded. If a TCP connection is closed by the remote site, the local application MUST be informed whether it closed normally or was aborted.

Two examples, take a look on Application Data below.

Without keepalive: without keepalive

With keepalive: with keepalive

查看更多
登录 后发表回答