nginx 反向代理有大量 TIME_WAIT 连接

nginx 反向代理有大量 TIME_WAIT 连接

我使用 nginx 作为反向代理,将一些流量重定向到 openfire XMPP 服务器。以下是我在 netstat 中看到的内容:

972 connections to port 80 from outside (includes website and redirected XMPP over http)
of them 231 in TIME_WAIT, seems like normal
272 connection from localhost:<misc port> to localhost:7070 (nginx to xmpp server), all ESTABLISHED
3154 connections from localhost:7070 to localhost:<misc port> (xmpp back to nginx)
of them 2882 in TIME_WAIT (1301 of them with expired timers) seems like it's not normal!

为什么有这么多?如何摆脱这些连接?

答案1

如果没有更多信息,很难确定原因。您可以ss列出所有连接:

ss --numeric -o state time-wait | less

按州统计连接数:

netstat -tan  | awk '{print $6}' | sort | uniq -c

Linux 内核有参数数量可能会影响连接数:

  • sysctl net.ipv4.tcp_keepalive_intv- isAlive 间隔探测之间的等待时间,默认值75,推荐值15-30
  • sysctl net.ipv4.tcp_tw_reuse- 当从协议角度来看是安全的时,允许重新使用处于 TIME_WAIT 状态的套接字来建立新连接。默认值为0(禁用)。
  • sysctl net.ipv4.tcp_fin_timeout- TCP/IP 释放已关闭的连接并重新使用其资源之前必须经过的时间
  • sysctl net.ipv4.tcp_keepalive_probes- 超时前的探测次数

每个工作进程nginx都保留其资源池,例如:

worker_processes 8;
events {
  worker_connections 768;
}
http {
  keepalive_timeout 65;
  server {
     keepalive_requests 1000;
     upstream backend {
        server 127.0.0.1:8080 max_conns=1024;
        # default value: none
        # keepalive 32;
     }
  }
}

上游文档

相关内容