nginx的负载均衡配置

Nginx 的负载均衡主要由两部分组成:

  • upstream 块:定义后端服务器集群(在 http 块内)。
  • server 块中的 proxy_pass:将请求转发到定义的 upstream 组。

基础模板 (nginx.conf)

bash 复制代码
http {
    # ==========================================
    # 1. 定义后端服务器集群 (Upstream)
    # ==========================================
    upstream my_backend_pool {
        # 默认策略:轮询 (Round Robin)
        server 192.168.1.101:8080;
        server 192.168.1.102:8080;
        
        # 其他策略参数见下文详解
    }

    # ==========================================
    # 2. 配置虚拟主机 (Server)
    # ==========================================
    server {
        listen 80;
        server_name example.com; # 域名或 IP

        location / {
            # 将请求转发到上面定义的 upstream 集群
            proxy_pass http://my_backend_pool;

            # 重要:传递真实用户 IP 给后端
            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 5s;
            proxy_send_timeout 10s;
            proxy_read_timeout 10s;
        }
    }
}

2. 常用负载均衡策略

upstream 块中,通过添加不同指令来切换策略。

A. 加权轮询 (Weighted Round Robin) - 最常用

根据服务器性能分配权重,权重越高,分配的请求越多。

bash 复制代码
upstream weighted_pool {
    server 192.168.1.101:8080 weight=3; # 性能强,承担 3/4 流量
    server 192.168.1.102:8080 weight=1; # 性能弱,承担 1/4 流量
}

B. IP 哈希 (IP Hash) - 会话保持

同一个 IP 的请求始终转发到同一台服务器,解决 Session 共享问题。

bash 复制代码
upstream ip_hash_pool {
    ip_hash; # 开启 IP 哈希
    server 192.168.1.101:8080;
    server 192.168.1.102:8080;
}

C. 最少连接 (Least Connections)

将新请求转发给当前连接数最少的服务器,适合长连接场景。

bash 复制代码
upstream least_conn_pool {
    least_conn; # 开启最少连接
    server 192.168.1.101:8080;
    server 192.168.1.102:8080;
}

D. 备用服务器 (Backup)

当主服务器全部不可用时,才使用备用服务器。

bash 复制代码
upstream backup_pool {
    server 192.168.1.101:8080;
    server 192.168.1.102:8080 backup; # 仅在主节点故障时启用
}
  1. 高级优化配置 (健康检查与超时)

在生产环境中,建议配置故障检测参数,自动剔除坏掉的节点

bash 复制代码
upstream optimized_pool {
    # 正常节点
    server 192.168.1.101:8080 max_fails=3 fail_timeout=30s;
    server 192.168.1.102:8080 max_fails=3 fail_timeout=30s;
    
    # 参数解释:
    # max_fails=3: 失败 3 次后标记为不可用
    # fail_timeout=30s: 标记不可用后,暂停分发 30 秒,30 秒后再尝试探测
    
    # 长连接复用 (提升性能)
    keepalive 32; 
}

server {
    location / {
        proxy_pass http://optimized_pool;
        
        # 配合 keepalive 使用,必须设置 HTTP/1.1 并关闭 Connection 头
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
相关推荐
小飞侠在吗2 小时前
博客GitHub链接时效性与可用性保障方案(适配国内访问)
github
Flandern111115 小时前
Pull Requests(PR)
学习·github·pr
网络研究院16 小时前
AgentGG:开源的代理式 SAST 扫描器
开源·github·工具·网络研究观·agentgg
前端程序猿i17 小时前
Nginx 教程:从入门到能上线
运维·nginx
明辰之林17 小时前
Nginx 1.26.2 → 1.30.2 升级指南(离线环境)
nginx
凤炎忻18 小时前
【GitHub】GitHub Actions 快速入门
github·自动化运维
逛逛GitHub18 小时前
YouTube 一哥手搓了个 AI 工作台,一周就 5 万多 Star 。
github
七牛云行业应用18 小时前
Codex CLI 和 Codex 桌面端完整教程:两种入口的功能对比与选择指南
前端·后端·github
小雨青年19 小时前
GitHub Spark:自然语言能把全栈 AI 应用做到什么程度
人工智能·github
阿里嘎多学长19 小时前
2026-06-08 GitHub 热点项目精选
开发语言·程序员·github·代码托管