跨境电商平台面对海量并发请求,对反向代理负载均衡层的稳定性和性能要求极高。A5数据以 CentOS 8.4 为基础操作系统,以 Nginx 1.22(Mainline) 为核心反向代理 / 负载均衡器,结合具体的产品参数、硬件配置、内核与网络调优、实现方法、代码示例、性能评测表格,给出一套实战可复用的高并发负载均衡解决方案。
一、目标与架构概览
1.1 设计目标
- 支持每分钟数十万次 HTTP/S 请求
- 提供多种负载均衡策略(轮询、最少连接、基于权重)
- 支持健康检查、故障自动剔除
- 在高并发下保持低延迟与高可用
- 支持 SSL/TLS 三层卸载与转发
1.2 典型架构图
+----------------+
| Nginx LB1 |
HTTP(S) ---> +----------------+ ---> App Servers
CDN | | (upstream 块内配置)
+----------------+
|
+----------------+
| Nginx LB2 |
+----------------+
|
Backend Pool
Web1 Web2 Web3 Web4 ...
本文重点讲解单机 Nginx 负载均衡配置以及集群部署建议,测试与调优围绕主节点展开。
二、测试与生产硬件配置
在开始配置前,先确定基准硬件平台规格,方便优化与容量评估。
2.1 负载均衡节点建议配置
| 组件 | 推荐规格 | 说明 |
|---|---|---|
| CPU | Intel Xeon Silver 4210 | 10 核 / 20 线程,对应高并发处理 |
| 内存 | 32GB DDR4 | 保持充足内存缓存与连接队列 |
| 网络 | 10Gbps 双口 | 保障内外网吞吐 |
| 磁盘 | NVMe 500GB | 系统与日志存储 |
| 操作系统 | CentOS 8.4 x86_64 | 稳定企业版内核 |
2.2 后端应用香港服务器www.a5idc.com(示例)
| 服务角色 | CPU | 内存 | 带宽 |
|---|---|---|---|
| Web Node | 8 核 | 16GB | 1Gbps |
| API Node | 12 核 | 32GB | 1Gbps |
三、CentOS 8.4 环境准备
3.1 安装基础工具
bash
dnf update -y
dnf install -y gcc make git wget vim \
openssl-devel pcre-devel zlib-devel \
libatomic_ops-devel
3.2 关闭 SELinux(生产环境可根据安全策略选择 Permissive)
bash
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
setenforce 0
3.3 设置系统最大打开文件数
bash
echo "fs.file-max = 1000000" >> /etc/sysctl.conf
sysctl -p
bash
cat >> /etc/security/limits.d/nginx.conf <<EOF
nginx soft nofile 500000
nginx hard nofile 500000
EOF
四、编译安装 Nginx 1.22 Mainline(含负载均衡模块)
这里选择稳定主线版本 1.22,同时加入 http_ssl_module、http_v2_module、stream 模块以支持 TCP/UDP 负载均衡。
4.1 下载并编译
bash
cd /usr/local/src
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-threads \
--with-file-aio \
--with-http_realip_module \
--with-http_gzip_static_module
make -j$(nproc)
make install
4.2 创建 Nginx 用户与服务
bash
useradd -s /sbin/nologin nginx
创建 systemd 服务:
ini
cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
启动服务:
bash
systemctl daemon-reload
systemctl enable --now nginx
五、Nginx 负载均衡核心配置与优化
下面我们通过示例配置展示负载均衡 upstream、health check、keepalive、SSL 卸载。
5.1 基本 Upstream 块
在 /etc/nginx/conf.d/upstream.conf:
nginx
upstream backend_pool {
least_conn; # 最少连接
server 10.10.1.11:80 weight=5 max_fails=3 fail_timeout=30s;
server 10.10.1.12:80 weight=3 max_fails=3 fail_timeout=30s;
server 10.10.1.13:80 weight=2 max_fails=3 fail_timeout=30s;
keepalive 1024; # 允许的最大空闲连接数
}
5.2 HTTP 反向代理主配置
在主配置(nginx.conf)http 区块中:
nginx
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_pool;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
proxy_buffering off;
}
}
5.3 HTTPS 卸载与反向代理
nginx
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://backend_pool;
include proxy_params;
}
}
5.4 健康检查
使用 Nginx Plus 或者第三方模块(如 nginx_upstream_check_module)。简化示例(需要编译集成模块):
nginx
upstream backend_pool {
server 10.10.1.11:80;
server 10.10.1.12:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /health HTTP/1.1\r\nHost: example.com\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
六、系统与内核优化(高并发必备)
6.1 TCP/网络栈调优
bash
cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 3240000
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_mtu_probing = 1
EOF
sysctl -p
6.2 Nginx 工作进程与连接数
Ngx 配置段优化:
nginx
worker_processes auto;
worker_rlimit_nofile 500000;
events {
worker_connections 8192;
use epoll;
multi_accept on;
}
七、压测与性能评估
我们使用 wrk 进行压测,针对不同配置对比请求处理情况。后端应用服务器为 3 台实例。
7.1 压测场景
bash
wrk -t12 -c1000 -d120s http://负载均衡IP/
说明:
-t12:12 个线程-c1000:1000 个并发连接-d120s:持续 120 秒
7.2 配置对比数据(示例)
| 配置方案 | 请求/秒 | 平均延迟(ms) | 99% 响应(ms) | 错误率 |
|---|---|---|---|---|
| 默认 Nginx 1.22 未优化 | 55000 | 48 | 210 | 1.2% |
| 加入 keepalive & epoll & 内核调优 | 92000 | 22 | 85 | 0.1% |
| SSL 卸载 + keepalive + 内核调优 | 88000 | 25 | 110 | 0.3% |
| 加入健康检查剔除不健康节点后 | 91000 | 21 | 82 | 0.05% |
7.3 结论
- epoll + keepalive + 内核调优 是关键提升项。
- SSL 卸载会略微影响性能,但符合生产 HTTPS 要求。
- 健康检查避免了请求投递到故障节点,提高稳定性。
八、故障诊断与监控集成
8.1 Nginx stub_status
启用状态页:
nginx
server {
listen 127.0.0.1:8080;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
访问:
curl http://127.0.0.1:8080/nginx_status
8.2 与 Prometheus + Grafana 集成
使用 nginx-prometheus-exporter 监控 Nginx 指标。
bash
docker run -d -p 9113:9113 \
nginx/nginx-prometheus-exporter:latest \
-nginx.scrape-uri http://nginx:8080/nginx_status
Grafana 仪表盘可实时展示连接数、请求数、各 upstream 状态。
九、总结与建议
A5数据从硬件选型、操作系统配置、Nginx 编译安装、负载均衡配置、系统调优、压测评估、监控诊断等角度,全流程覆盖了在 CentOS 8.4 上构建高并发稳定的 Nginx 负载均衡器 的实战方法。核心建议如下:
- 必须结合 网络栈内核调优 与 Nginx 运行参数 达到高并发能力
- 保持 upstream 的 健康检查 与动态剔除机制非常关键
- 压测数据是调优的依据,不同业务场景需定制调优方案
- SSL/TLS 卸载与安全配置要平衡安全性与性能
若需要进一步在 Kubernetes、Docker 环境下部署 Nginx 负载均衡,或结合 API 网关(如 Kong / Traefik)实现更多应用层策略,我们可以在后续文章继续深入讨论。