# Nginx Load Balancer Configuration for Nacos Cluster
# Nacos 2.x uses gRPC which requires HTTP/2 and long-lived connections

worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    include /etc/nginx/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;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 100M;

    # 上游Nacos集群
    upstream nacos_cluster {
        server nacos-server-1:8848 max_fails=3 fail_timeout=30s;
        server nacos-server-2:8848 max_fails=3 fail_timeout=30s;
        server nacos-server-3:8848 max_fails=3 fail_timeout=30s;
        
        # 负载均衡策略
        least_conn;  # 最少连接优先
        
        # 连接保活
        keepalive 32;
    }

    # gRPC上游（用于Nacos 2.x客户端gRPC通信）
    upstream nacos_grpc {
        server nacos-server-1:9848 max_fails=3 fail_timeout=30s;
        server nacos-server-2:9848 max_fails=3 fail_timeout=30s;
        server nacos-server-3:9848 max_fails=3 fail_timeout=30s;
        
        keepalive 32;
    }

    server {
        listen 8848;
        proxy_pass nacos_cluster;
        
        # 超时配置（Nacos需要长连接）
        proxy_connect_timeout 10s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # 缓冲配置
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        
        # 头信息转发
        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-Proto $scheme;
        
        # Nacos特定头
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        # 健康检查相关状态
        location /nacos/v1/console/health/readiness {
            proxy_pass http://nacos_cluster;
            proxy_connect_timeout 5s;
            proxy_send_timeout 10s;
            proxy_read_timeout 10s;
            access_log off;
        }

        location /nacos/v1/console/health/liveness {
            proxy_pass http://nacos_cluster;
            proxy_connect_timeout 5s;
            proxy_send_timeout 10s;
            proxy_read_timeout 10s;
            access_log off;
        }
    }

    # gRPC代理（用于Nacos 2.x客户端通信）
    # 注意：gRPC需要HTTP/2支持
    server {
        listen 9848 http2;
        
        grpc_pass grpc://nacos_grpc;
        
        # gRPC超时
        grpc_connect_timeout 10s;
        grpc_send_timeout 60s;
        grpc_read_timeout 60s;
        
        # 头信息
        grpc_set_header Host $host;
        grpc_set_header X-Real-IP $remote_addr;
        grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # 另一个gRPC端口（集群间通信9849）
    server {
        listen 9849 http2;
        
        grpc_pass grpc://nacos_grpc;
        
        grpc_connect_timeout 10s;
        grpc_send_timeout 60s;
        grpc_read_timeout 60s;
        
        grpc_set_header Host $host;
        grpc_set_header X-Real-IP $remote_addr;
    }
}
