在高并发、大流量的互联网应用场景中,Nginx 负载均衡是保障系统稳定性的核心方案。该功能可将海量客户端请求均匀分发至多个后端服务节点,避免单节点过载造成的性能瓶颈、服务卡顿甚至宕机问题,同时大幅提升系统的响应速度与可用性。
高并发场景下,请求集中访问少数服务节点会直接导致节点过载,引发整个系统瘫痪。Nginx 基于 upstream 模块实现负载均衡,可智能分配请求流量,让所有后端服务器均匀承担业务压力,最大化利用服务器资源,同时实现服务的横向扩容。
Nginx 负载均衡依托 upstream 模块实现,该模块用于定义后端服务器集群,配合 proxy_pass 完成请求转发。以 3 个 Spring Boot 服务节点为例,基础配置如下:
# 定义后端服务集群,默认采用轮询策略
upstream hello_nginx {
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}
server {
listen 80;
server_name localhost;
location / {
# 转发请求至负载均衡集群
proxy_pass http://hello_nginx;
# 传递客户端真实请求头
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 支持 WebSocket 长连接
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}Nginx 内置多种负载均衡策略,适配不同业务场景,以下为企业常用方案及配置:
Nginx 默认策略,无需额外配置,请求按顺序轮流分配给后端节点,适用于服务器配置一致、短连接场景。
upstream hello_nginx {
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}为性能更高的服务器配置更高权重,权重越高,分配的请求越多,适用于服务器硬件配置不一致的场景。
upstream hello_nginx {
server helloNginx1:8080 weight=1;
server helloNginx2:8080 weight=2;
server helloNginx3:8080 weight=1;
}将请求分配给当前活跃连接数最少的节点,避免节点因长连接堆积过载,适用于长连接、耗时较长的业务场景。
upstream hello_nginx {
least_conn;
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}根据客户端 IP 进行哈希计算,固定 IP 永久访问同一台后端节点,实现会话持久化,适用于需要登录状态保持的场景。
upstream hello_nginx {
ip_hash;
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}根据请求 URL 进行哈希计算,固定资源访问同一节点,适用于静态资源缓存、文件存储场景。
upstream hello_nginx {
hash $request_uri;
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}在 URL 哈希基础上增加一致性算法,节点上下线时仅影响少量请求,适用于大规模服务集群、高可用场景。
upstream hello_nginx {
hash $request_uri consistent;
server helloNginx1:8080;
server helloNginx2:8080;
server helloNginx3:8080;
}Nginx 支持为后端节点配置状态参数,实现故障隔离、备用节点等企业级功能:
upstream hello_nginx {
server helloNginx1:8080 max_fails=3 fail_timeout=30s;
server helloNginx2:8080 backup;
server helloNginx3:8080 down;
}max_fails=3:最大失败次数,连续 3 次请求失败则标记节点故障
fail_timeout=30s:故障超时时间,30 秒内不再向故障节点发送请求
backup:标记为备用节点,仅主节点全部故障时启用
down:永久标记节点下线,不参与负载均衡
通过 Docker Compose 一键部署 Nginx + 3 个 Spring Boot 服务,优化时区、延迟启动等问题,适配生产环境。
services:
nginx:
image: nginx:1.26.2-alpine
restart: always
container_name: nginx
ports:
- 80:80
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- helloNginx1
- helloNginx2
- helloNginx3
helloNginx1:
hostname: helloNginx1
image: eclipse-temurin:17-jre-alpine
restart: always
container_name: helloNginx1
environment:
TZ: Asia/Shanghai
volumes:
- ./hello-nginx.jar:/home/hello-nginx.jar
command: sh -c 'sleep 5 && java -jar /home/hello-nginx.jar'
helloNginx2:
hostname: helloNginx2
image: eclipse-temurin:17-jre-alpine
restart: always
container_name: helloNginx2
environment:
TZ: Asia/Shanghai
volumes:
- ./hello-nginx.jar:/home/hello-nginx.jar
command: sh -c 'sleep 5 && java -jar /home/hello-nginx.jar'
helloNginx3:
hostname: helloNginx3
image: eclipse-temurin:17-jre-alpine
restart: always
container_name: helloNginx3
environment:
TZ: Asia/Shanghai
volumes:
- ./hello-nginx.jar:/home/hello-nginx.jar
command: sh -c 'sleep 5 && java -jar /home/hello-nginx.jar'# 负载均衡集群配置
upstream hello_nginx {
least_conn;
server helloNginx1:8080 max_fails=3 fail_timeout=30s;
server helloNginx2:8080 max_fails=3 fail_timeout=30s;
server helloNginx3:8080 backup;
}
# 虚拟主机配置
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://hello_nginx;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
}user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
# 加载子配置
include /etc/nginx/conf.d/*.conf;
}配置规范:conf.d 目录下仅编写业务相关配置,全局配置保留在主配置文件中
健康检查:生产环境必须配置 max_fails 和 fail_timeout,自动隔离故障节点
容器部署:添加延迟启动、时区配置,避免服务启动失败
会话保持:登录类业务优先使用 ip_hash 策略,防止会话丢失
平滑更新:修改负载均衡配置后,使用 reload 热加载,禁止直接重启服务