# ============================================================
# Nginx 负载均衡 - 加权轮询配置示例
# ============================================================
#
# 功能说明:
# - 按权重分配请求比例
# - 适用于服务器性能差异场景
# - 权重越大，分配请求越多
#
# Lolly 对应配置:
# server:
#   proxy:
#     - path: "/api"
#       targets:
#         - url: "http://backend1:8080"
#           weight: 5
#         - url: "http://backend2:8080"
#           weight: 3
#         - url: "http://backend3:8080"
#           weight: 2
#       load_balance: "weighted_round_robin"
# ============================================================

upstream backend {
    # 加权轮询
    # Lolly 对应: load_balance: "weighted_round_robin"，每个 target 设置 weight

    # 权重比例 5:3:2 = backend1 处理 50% 请求
    server backend1:8080 weight=5;
    server backend2:8080 weight=3;
    server backend3:8080 weight=2;

    # 备用服务器（权重为 0，仅在主服务器故障时启用）
    server backend4:8080 backup;

    keepalive 32;
}

server {
    listen 80;
    server_name weighted.example.com;

    location /api {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

# 权重计算示例:
#
# 总权重 = 5 + 3 + 2 = 10
#
# 每 10 个请求的分配:
# backend1: 5 个请求 (50%)
# backend2: 3 个请求 (30%)
# backend3: 2 个请求 (20%)
#
# 实际分配序列: 1,1,1,1,1,2,2,2,3,3, 1,1,1,1,1,2,2,2,3,3...
#
# 使用场景:
# 1. 高性能服务器分配更多请求
# 2. 新服务器逐步上线（先设置低权重）
# 3. 混合部署（物理机权重高，虚拟机权重低）