配置linux并发连接最大连接数

背景

linux机器默认的并发连接数大概为1000左右,高并发是难以支持的,所以要修改linux系统的各项参数

设置open files

查看open files:unlimit -a

临时设置open files数:ulimit -n [期望最大的open files数值]

永久设置open files数:/etc/security/limits.conf文件中添加如下两行

1
2
* soft nofile 50000
* hard nofile 50000

PS:ubuntu系统不能用*,必须指定用户

设置tcp优化

/etc/sysctl.conf文件中添加如下内容之后,执行sysctl -p启用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
net.core.rmem_default = 256960
net.core.rmem_max = 513920
net.core.wmem_default = 256960
net.core.wmem_max = 513920
net.core.netdev_max_backlog = 2000
net.core.somaxconn = 2048
net.core.optmem_max = 81920
net.ipv4.tcp_mem = 131072 262144 524288
net.ipv4.tcp_rmem = 8760 256960 4088000
net.ipv4.tcp_wmem = 8760 256960 4088000
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 2048

测试

使用这个c1000k工具来测试你的系统最多支持多少并发连接.