# ============================================================
# Nginx 反向代理基础配置示例
# ============================================================
#
# 功能说明:
# - 反向代理到后端服务
# - 请求头修改（X-Real-IP, X-Forwarded-For 等）
# - 超时控制
# - 后端健康检查
#
# Lolly 对应配置:
# server:
#   proxy:
#     - path: "/api"
#       targets:
#         - url: "http://backend1:8080"
#           weight: 3
#         - url: "http://backend2:8080"
#           weight: 1
#       load_balance: "round_robin"
#       timeout:
#         connect: 5s
#         read: 30s
#         write: 30s
#       headers:
#         set_request:
#           X-Real-IP: "$remote_addr"
#           X-Forwarded-For: "$proxy_add_x_forwarded_for"
#           X-Forwarded-Host: "$host"
#           X-Forwarded-Proto: "$scheme"
#         set_response:
#           X-Server: "lolly"
#         remove: ["X-Powered-By"]
# ============================================================

# 后端服务器组定义
# Lolly 对应: targets 列表，load_balance 指定算法
upstream backend {
    # 负载均衡算法（默认轮询）
    # 其他选项:
    # least_conn;      # 最少连接 - Lolly: least_conn
    # ip_hash;         # IP 哈希 - Lolly: ip_hash
    # hash $request_uri consistent;  # 一致性哈希 - Lolly: consistent_hash

    server backend1:8080 weight=3;
    server backend2:8080 weight=1;
    server backend3:8080 backup;  # 备用服务器

    # 健康检查（需要 nginx Plus 或第三方模块）
    # Lolly 对应: health_check 配置
    # health_check interval=10s fails=3 passes=2 uri=/health;

    # 连接保持
    keepalive 32;  # 保持 32 个空闲连接
}

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

    # 客户端请求体大小限制
    # Lolly 对应: client_max_body_size: "50MB"
    client_max_body_size 50m;
    client_body_buffer_size 128k;

    # 代理缓冲配置
    proxy_buffering on;
    proxy_buffer_size 4k;
    proxy_buffers 8 32k;
    proxy_busy_buffers_size 64k;

    # API 代理
    location /api {
        proxy_pass http://backend;

        # 请求头设置
        # Lolly 对应: headers.set_request
        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 X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Request-ID $request_id;

        # 响应头设置
        # Lolly 对应: headers.set_response
        add_header X-Server nginx always;

        # 移除响应头
        # Lolly 对应: headers.remove
        proxy_hide_header X-Powered-By;

        # 超时配置
        # Lolly 对应: timeout 配置
        proxy_connect_timeout 5s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;

        # 下游服务器故障转移
        # 当后端返回 502/503/504 时重试下一个
        # Lolly 对应: next_upstream 配置
        proxy_next_upstream error timeout http_502 http_503 http_504;
        proxy_next_upstream_tries 3;
        proxy_next_upstream_timeout 30s;

        # HTTP 版本
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }

    # WebSocket 代理示例
    # Lolly 支持 WebSocket 代理
    location /ws {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;

        # WebSocket 长连接超时
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}