服务器(比如:HTTP)出现大量TIME_WAIT的解决方法

       对TCP几种状态了解的同学,应该都清楚,tcp主动断开连接的一方会出现TIME_WAIT状态,TIME_WAIT状态会保持2MSL的时间,这个主要是用来防止,主动断开的一方发送对最后一个FIN的ACK丢失,导致被动passive close的一方的LAST-ACK定时器超时重传FIN,避免TCP状态的混乱。但是在服务器大并发的情况下,这种默认的机制可能会导致服务器维护大量的TIME_WAIT状态,导致大量资源的浪费,可能会影响后续连接和请求的处理。为了解决这一现象,我们可以通过修改服务器的配置,这里以我们在阿里云上某台Linux服务器的配置为例。

[root@izbp1dnvbin1s4fqqr8izfz ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.file-max = 1000000
net.core.somaxconn = 2048
net.core.rmem_default = 16777216
net.core.wmem_default = 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 20000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_orphans = 131072
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_tw_buckets = 40000

vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
       这里主要可以通过修改net.ipv4.tcp_fin_timeout(FIN超时时间,主要用于主动挂断的一方,在FIN-WAIT2状态保持的时间)、net.ipv4.tcp_tw_reuse(time_wait状态的socket重用,允许将socket重新用于新的TCP连接,默认为0,表示关闭)、net.ipv4.tcp_tw_recycle(是否开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭)等参数,然后使用/sbin/sysctl -p命令使配置生效。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值