EMQX的服务器调优
文章目录
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
说明:在Centos7系统中,
/etc/security/limits.conf
文件的配置作用域缩小了,其只适用于通过PAM认证登录用户的资源限制。而systemd service的资源设置,则需修改全局配置/etc/systemd/system.conf
- 全局配置
# 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 协议栈网络参数
在
/etc/sysctl.conf
中添加如下参数:
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
说明:
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
2.6 FIN-WAIT-2 Socket 超时设置
net.ipv4.tcp_max_tw_buckets=1048576
2.7 立即生效
同样执行如下命令立即生效:
# sysctl -p
3. 总结
file-max=2097152
fs.nr_open=2097152
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.ipv4.ip_local_port_range = 1024 65535
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
net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
net.ipv4.tcp_max_tw_buckets=1048576
net.ipv4.tcp_max_tw_buckets=1048576