大网数据广告活动
服务器运维问答

nginx detecting dead clients

· 52CE 我爱测

Normally when you pull the network cable out of a network device (which simulates some drop on the network, so an obscure disconnect), the underlaying sockets do not detect a disconnect (at least our units don't) so we use keep a…

问题原文

原帖链接 · Lonko · CC BY-SA 4.0

Normally when you pull the network cable out of a network device (which simulates some drop on the network, so an obscure disconnect), the underlaying sockets do not detect a disconnect (at least our units don't) so we use keep alive to drop connection and reconnect.

All units use hourly long poll to nginx server. So my question is:

If the unit does not detect a cable disconnect (and probably 100 other situations on the network), then also nginx does not disconnect the socket and that socket stays open?

Stay open for how long?

Should we use on the nginx side also tcp keep alive towards client? How?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

You need TCP keepalive in order to detect outages like that. Socket is only an abstraction, and socket closes only when packet transmission in the connection fails.

In long poll situation, there is no packet transmission, so there is no packet loss event for TCP state machine to react to.

Therefore you need to enable TCP keepalive on the network stack:

sysctl -w net.ipv4.tcp_keepalive_time = 600
sysctl -w net.ipv4.tcp_keepalive_intvl = 60
sysctl -w net.ipv4.tcp_keepalive_probes = 5

First value specifies how long connection must be idle before a keepalive probe is sent.

Second value specifies how often the keepalive probe should be sent once sending is triggered.

Last value means how many probe packets must fail before the connection is considered broken.

The sysctl only modifies running configuration. You need to refer to your distribution's documentation to see how to make it persistent.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-12-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。