# Redirect HTTP to HTTPS
server {
    listen 80;
    server_name luminacloud.ai;

    location / {
        return 301 https://$host$request_uri;
    }
}

upstream api_servers {
    server one-api-1:3001 weight=1 max_fails=3 fail_timeout=30s;
    server one-api-2:3001 weight=1 max_fails=3 fail_timeout=30s;
    server one-api-3:3001 backup;
}

server {
    listen 443 ssl;
    server_name luminacloud.ai;

    ssl_certificate /etc/secret/luminacloud.ai.crt;
    ssl_certificate_key /etc/secret/luminacloud.ai.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM_SHA384';
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://api_servers;
        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;

        # CORS header
        add_header 'Access-Control-Allow-Origin' $cors_allow_origin always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;

        # 处理 OPTIONS 预检请求
        if ($request_method = 'OPTIONS') {
            return 204;
        }
    }
}
