# 编辑网络接口配置文件
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# 添加或修改以下参数
ETHTOOL_OPTS="-G ${DEVICE} rx 4096 tx 4096"
ETHTOOL_OPTS="-K ${DEVICE} rx on tx on sg on tso on gso on gro on lro on"
# 编辑sysctl.conf
vi /etc/sysctl.conf
# 添加以下内容
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# 使配置生效
sysctl -p
vi /etc/sysctl.conf
# 增大TCP连接数
net.ipv4.tcp_max_syn_backlog = 8192
net.core.somaxconn = 8192
# TIME_WAIT优化
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
# 内存相关
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
# 其他TCP优化
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_slow_start_after_idle = 0
# 文件描述符限制
fs.file-max = 65535
sysctl -p
# 编辑nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
use epoll;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100;
types_hash_max_size 2048;
server_tokens off;
}
# 编辑httpd.conf
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
EnableSendfile on
EnableMMAP on
# 编辑sysctl.conf添加
net.netfilter.nf_conntrack_max = 655360
net.nf_conntrack_max = 655360
# 对于高并发服务器
echo 1200000 > /proc/sys/net/netfilter/nf_conntrack_max
# 允许HTTP/HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# 调整防火墙规则处理顺序
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 10 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# 查看网络连接状态
ss -s
netstat -s
# 查看TCP连接状态
ss -antp
# 监控网络流量
iftop -n
nload
# 查看中断平衡
cat /proc/interrupts
启用BBR拥塞控制算法(CentOS 7+):
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
调整IRQ平衡:
yum install irqbalance
systemctl enable irqbalance
systemctl start irqbalance
考虑使用高性能网络驱动:
ethtool -i eth0 # 查看当前驱动
对于虚拟化环境,考虑启用virtio-net和vhost_net:
modprobe vhost_net
echo "vhost_net" >> /etc/modules-load.d/vhost_net.conf
通过以上优化措施,可以显著提升CentOS Web服务器的网络性能和并发处理能力。建议根据实际业务负载进行参数调整,并在生产环境部署前进行充分的测试。