# ========= S3 API 主站域名 ========== 
upstream cloud {
    server 127.0.0.1:9000;  # MinIO S3 API
    keepalive 64;
}

# ========= 控制台子域名 ========== 
upstream admin {
    server 127.0.0.1:9001;  # MinIO 控制台 Web UI
    keepalive 64;
}

# ----------------------------
# ✅ 控制台路径反代，保留原逻辑：https://s3.szaiai.com/s3
# ----------------------------
server {
    server_name s3.szaiai.com;

    client_max_body_size 100M;

    location / {
        return 404;
    }

    # ✅ 干净路径，支持程序、curl、mc、Range
    location ~ ^/[^/]+/ {
        proxy_pass http://cloud;
        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;
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;
    }

    # ✅ 保留原 /s3/ 路由，给控制台 UI 使用
    location /s3/ {
        proxy_pass http://cloud/;
        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;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/s3.szaiai.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/s3.szaiai.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    server_name s3.szaiai.com;
    return 301 https://$host$request_uri;
}

# ----------------------------
# ✅ 控制台 UI 域名：https://console.szaiai.com
# ----------------------------
server {
    server_name console.szaiai.com;

    client_max_body_size 5G;

    location / {
        proxy_pass http://admin/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/console.szaiai.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/console.szaiai.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    server_name console.szaiai.com;
    return 301 https://$host$request_uri;
}

# ----------------------------
# ✅ 新增：干净的 API 域名，用于 mc/share/download：https://api.szaiai.com
# ----------------------------
server {
    server_name api.szaiai.com;

    client_max_body_size 5G;

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

        # ✅ 补上支持 Range 请求的 Header 转发
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;        
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/api.szaiai.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.szaiai.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    server_name api.szaiai.com;
    return 301 https://$host$request_uri;
}
