Operations 11 min read

Why TCP KeepAlive Matters: Solving LVS and Nginx Timeout Issues

This article explains the TCP KeepAlive mechanism, why load balancers like LVS and F5 drop idle connections after 90 seconds, how to configure Linux kernel and socket options, and the correct way to enable TCP KeepAlive in Nginx to prevent premature disconnects.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why TCP KeepAlive Matters: Solving LVS and Nginx Timeout Issues

During a recent work scenario the author encountered a problem where a client accessed a service through an LVS virtual IP, which forwarded traffic to an Nginx server. When the Nginx backend took longer than 90 seconds to respond, LVS reset the TCP connection because its default session timeout is 90 seconds.

The same behavior can appear on commercial load balancers such as F5, which also terminate idle sessions after a timeout, though they may send a RESET only when the client later transmits data.

To address this, the author explores TCP KeepAlive, a mechanism that periodically sends empty packets to verify that the peer is still reachable. The article first clarifies that TCP itself has no concept of “request”; that belongs to higher‑level protocols like HTTP.

Why KeepAlive Is Needed

When a TCP connection remains idle for a long period, KeepAlive probes allow both ends to detect a dead peer and close the socket gracefully, preventing load‑balancer resources from being exhausted.

How to Enable KeepAlive on Linux

Linux does not have a global switch for TCP KeepAlive; it must be enabled per socket. Three kernel parameters influence the behavior:

net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_time = 7200

These values control the interval between probes, the number of probes, and the idle time before the first probe (in seconds).

Applications can also set socket‑level options via setsockopt:

TCP_KEEPCNT  // overrides tcp_keepalive_probes
TCP_KEEPIDLE // overrides tcp_keepalive_time
TCP_KEEPINTVL // overrides tcp_keepalive_intvl

For example, if the kernel default keepalive time is 7200 s but an application sets TCP_KEEPIDLE to 60, the first probe will be sent after 60 seconds of inactivity.

TCP KeepAlive vs. HTTP Keep‑Alive

TCP KeepAlive and HTTP Keep‑Alive are unrelated. TCP KeepAlive works at the transport layer to verify link liveness, while HTTP Keep‑Alive (or Connection: keep-alive) allows multiple HTTP requests to reuse the same TCP connection, reducing the overhead of establishing new connections.

Configuring TCP KeepAlive in Nginx

The author initially tried to set keepalive_timeout in Nginx, which only affects HTTP keep‑alive, not TCP keepalive. By inspecting Nginx source code, the correct way to enable TCP KeepAlive is to use the so_keepalive parameter on the listen directive, e.g.: listen 80 so_keepalive=60s; Setting this option resolved the LVS timeout problem, allowing long‑running requests to complete without the connection being reset.

References

• "TCP/IP Illustrated, Volume 1" – for fundamental networking concepts.

• TCP Keepalive HOWTO

• Nginx Core Module Documentation

• Nginx source code: https://github.com/alibaba/tengine

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

load balancingTCPLinuxNginxKeepaliveLVS
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.