1. Linux 操作系统参数
1.1 file-max
- 临时设置(不推荐)
sysctl -w fs.file-max=2097152
- 持久化+即时生效(推荐)
vim /etc/sysctl.conf
fs.file-max=2097152
sysctl -p
1.2 ulimit
- 临时设置(不推荐)
# fs.file-max = 1048576
或
# ulimit -n 1048576
- 持久化设置
vim /etc/security/limits.conf
* soft nofile 1048576
* hard nofile 1048576
- 全局配置
# vim /etc/systemd/system.conf
DefaultLimitNOFILE=1048576
1.3 nr_open
- 持久化+即时生效(推荐)
vim /etc/sysctl.conf
fs.nr_open=2097152
sysctl -p
- 临时生效(不推荐)
sysctl -w fs.nr_open=2097152
或者
echo 2097152 > /proc/sys/fs/nr_open
2. TCP 协议栈网络参数
2.1 并发连接 backlog 设置
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
2.2 可用端口范围
net.ipv4.ip_local_port_range = 1024 65535
2.3 TCP Socket 读写 Buffer 设置
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.optmem_max=16777216
#net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem=1024 4096 16777216
net.ipv4.tcp_wmem=1024 4096 16777216
2.4 TCP 连接追踪设置
net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
2.5 TIME-WAIT Socket 最大数量、回收与重用设置
net.ipv4.tcp_max_tw_buckets=1048576
#以下两个不建议开启該设置,NAT 模式下可能引起连接 RST
#net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_reuse = 1
2.6 FIN-WAIT-2 Socket 超时设置
net.ipv4.tcp_max_tw_buckets=1048576
2.7 立即生效
# sysctl -p