← 返回主页
案例 01
高并发电商网站 Nginx 负载均衡架构设计

背景描述

某电商平台日常 QPS 5000,大促预计流量 10 倍增长。原有架构为单台 Nginx → 3 台 Tomcat,存在单点故障风险。需重新设计高可用负载均衡架构。

解决方案

采用 2 层 Nginx 架构:L4 层 2 台 Nginx + Keepalived VIP 做高可用和四层转发;L7 层 4 台 Nginx 做七层路由和 SSL 卸载。后端 Tomcat 集群扩至 12 台。

技术要点

  • L4 层 Nginx stream 模块做 TCP 443 转发,Keepalived 提供 VIP 漂移
  • L7 层 Nginx 做 SSL 终止、路径路由、限流和缓存
  • 负载均衡算法:least_conn(最小连接数),适合长短连接混合场景
  • 后端健康检查:proxy_next_upstream + 主动 health_check 模块
# L4 层 Nginx(nginx.conf stream 块) stream { upstream ssl_backend { least_conn; server 192.168.1.21:443 weight=5 max_fails=3 fail_timeout=30s; server 192.168.1.22:443 weight=5 max_fails=3 fail_timeout=30s; server 192.168.1.23:443 weight=5 max_fails=3 fail_timeout=30s; server 192.168.1.24:443 weight=5 max_fails=3 fail_timeout=30s; } server { listen 443; proxy_pass ssl_backend; proxy_connect_timeout 3s; } } # L7 层 Nginx upstream tomcat_cluster { least_conn; server 10.0.1.1:8080 weight=5 max_fails=3 fail_timeout=30s; server 10.0.1.2:8080 weight=5 max_fails=3 fail_timeout=30s; keepalive 64; } server { listen 443 ssl http2; ssl_certificate /etc/nginx/certs/site.crt; ssl_certificate_key /etc/nginx/certs/site.key; location / { proxy_pass http://tomcat_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout http_502 http_503; } # 静态资源缓存 location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ { proxy_pass http://tomcat_cluster; expires 30d; add_header Cache-Control "public, immutable"; } }
案例 02
HTTPS 证书自动化管理与 A+ 安全评级

背景描述

某客户运营着 15 个子域名网站,SSL 证书之前人工管理,经常忘记续期导致证书过期、用户浏览器报安全警告。需实现证书全自动申请和续期,同时提升 SSL 安全评级至 A+。

解决方案

使用 Certbot + Let's Encrypt 免费证书,配置自动续期 Cron Job。Nginx 配置强加密套件、HSTS、OCSP Stapling,通过 SSL Labs 测试获得 A+ 评级。

技术要点

  • Certbot DNS-01 验证方式支持通配符证书(*.example.com),无需开放 80 端口
  • SSL 协议仅启用 TLS 1.2 和 TLS 1.3,禁用旧版本
  • 加密套件优先使用 ECDHE 前向保密算法
  • HSTS max-age=63072000(2年),includeSubDomains,preload
# Certbot 通配符证书申请(DNS-01 验证) certbot certonly --manual --preferred-challenges dns \ -d "*.example.com" -d example.com \ --agree-tos --email admin@example.com # 自动续期 Cron(每天凌晨检查) 0 3 * * * certbot renew --quiet --post-hook "nginx -s reload" # Nginx SSL A+ 配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 1h; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; # A+ 关键项:HSTS + OCSP Stapling + 仅 TLS 1.2/1.3
案例 03
Nginx 502 大规模故障应急与根因分析

背景描述

某 SaaS 平台新版本上线 10 分钟后,全站出现 502 Bad Gateway。经排查,后端服务改为监听 8081 端口,但 Nginx upstream 配置仍是 8080。且健康检查未及时摘除异常节点,导致大量请求失败。

解决方案

紧急修正 upstream 端口并 reload。事后建立"部署检查清单":CI/CD 部署脚本增加端口一致性校验环节,上线前自动执行 Nginx -t 语法和 upstream 可达性检测。

技术要点

  • 故障快速定位:tail -f error.log 看到 connect() failed (111: Connection refused)
  • curl -v http://backend:8080 验证端口不通,确认配置未更新
  • 优化健康检查:主动检查 + 被动检查组合,故障转移时间从 30s 缩短到 3s
  • CI/CD 增加部署后自动验证脚本
# 故障排查命令 tail -100 /var/log/nginx/error.log | grep -E "502|upstream|connect" # 输出: connect() failed (111: Connection refused) while connecting to upstream # 验证后端可达性 for port in 8080 8081; do timeout 2 bash -c "echo >/dev/tcp/10.0.1.1/$port" 2>/dev/null \ && echo "Port $port: OK" || echo "Port $port: FAIL" done # 部署后自动验证脚本(CI/CD Pipeline 集成) nginx -t && echo "Syntax OK" for upstream in $(grep 'server ' /etc/nginx/conf.d/upstream.conf | awk '{print $2}' | tr -d ';'); do host=$(echo $upstream | cut -d: -f1) port=$(echo $upstream | cut -d: -f2) nc -z -w 2 $host $port || { echo "FAIL: $upstream unreachable"; exit 1; } done echo "All upstreams reachable"
案例 04
Nginx 动态限流与 CC 攻击防护

背景描述

某内容资讯站遭遇 CC 攻击,单 IP 发起大量请求耗尽后端资源。上 WAF 成本太高,需要基于 Nginx 快速构建低成本防护方案。

解决方案

使用 Nginx limit_req 模块做 IP 级请求频率限制,limit_conn 限制并发连接数。配合 map 模块实现动态黑白名单,通过 Lua 脚本记录恶意 IP 并自动封禁。

技术要点

  • limit_req_zone:基于 $binary_remote_addr 限制请求速率 10r/s
  • limit_conn:限制单 IP 并发连接数 20,防止连接耗尽
  • map + geo 实现 IP 白名单(内网/合作伙伴 IP 不限流)
  • 自定义 503 错误页返回友好提示而非空白页
# 限流配置 http { # 请求频率限制(10MB 共享内存,约 16 万 IP) limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s; # 并发连接限制 limit_conn_zone $binary_remote_addr zone=conn_limit:10m; # IP 白名单 geo $whitelist { default 0; 192.168.0.0/16 1; # 内网 10.0.0.0/8 1; # 内网 1.2.3.4 1; # 合作伙伴 IP } map $whitelist $limit { 0 $binary_remote_addr; 1 ""; } server { location / { limit_req zone=req_limit burst=20 nodelay; limit_req_status 429; limit_conn conn_limit 20; limit_conn_status 503; proxy_pass http://backend; } error_page 429 = @too_many_requests; location @too_many_requests { return 429 '{"error":"请求过于频繁,请稍后重试"}'; add_header Content-Type application/json; } } }