# ============================================================
# Nginx 负载均衡 - 轮询配置示例
# ============================================================
#
# 功能说明:
# - 默认轮询算法
# - 均匀分配请求到后端服务器
# - 连接保持优化
#
# Lolly 对应配置:
# server:
#   proxy:
#     - path: "/api"
#       targets:
#         - url: "http://backend1:8080"
#         - url: "http://backend2:8080"
#         - url: "http://backend3:8080"
#       load_balance: "round_robin"  # 默认值
# ============================================================

upstream backend {
    # 轮询（默认算法，无需指定）
    # Lolly 对应: load_balance: "round_robin"

    server backend1:8080;
    server backend2:8080;
    server backend3:8080;

    # 连接保持
    keepalive 32;

    # 保持连接超时
    keepalive_timeout 60s;

    # 每个连接最大请求数
    keepalive_requests 100;
}

server {
    listen 80;
    server_name lb.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;
    }
}

# 后端服务器状态参数:
#
# server backend:8080 weight=N;    # 权重
# server backend:8080 max_fails=N;  # 最大失败次数（默认 1）
# server backend:8080 fail_timeout=T;  # 失败超时（默认 10s）
# server backend:8080 backup;       # 备用服务器
# server backend:8080 down;         # 标记为不可用
#
# 示例:
# upstream backend {
#     server backend1:8080 max_fails=3 fail_timeout=30s;
#     server backend2:8080 backup;
# }