# /etc/nginx/conf.d/hololake.conf
# ═══════════════════════════════════════════════
# guanghulab.com 统一入口 · Nginx 配置模板
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
#
# 使用方法：
# 1. 复制到服务器: scp deploy/nginx/hololake.conf root@43.134.16.246:/etc/nginx/conf.d/
# 2. 如果使用 Let's Encrypt: certbot --nginx -d guanghulab.com -d www.guanghulab.com
# 3. 验证: nginx -t && systemctl reload nginx
#
# 服务器: 铸渊主权服务器 ZY-SVR-002 (腾讯云 · 新加坡二区)
# 旧服务器: 阿里云 8.155.62.235 (已弃用)
#
# 端口映射：
#   3721 → API 代理（AI 聊天，SSE 流式）
#   3002 → Persona Studio 后端
#   3000 → Express 主后端
#   3001 → HLI 中间层
#   3800 → 铸渊主权服务器 API
#   3900 → GLADA 自主开发Agent
#   8080 → WebSocket（状态看板实时推送）

# HTTP → HTTPS 重定向（启用 HTTPS 后取消注释）
# server {
#     listen 80;
#     server_name guanghulab.com www.guanghulab.com;
#     return 301 https://$host$request_uri;
# }

server {
    listen 80;
    # listen 443 ssl;  # 启用 HTTPS 后取消注释
    server_name guanghulab.com www.guanghulab.com;

    # Let's Encrypt 证书（certbot 自动填充，启用 HTTPS 后取消注释）
    # ssl_certificate /etc/letsencrypt/live/guanghulab.com/fullchain.pem;
    # ssl_certificate_key /etc/letsencrypt/live/guanghulab.com/privkey.pem;

    root /var/www/guanghulab;
    index index.html;

    # ── 前端静态文件（主站首页 = docs/index.html，已复制到根目录）──
    location / {
        try_files $uri $uri/ /index.html;
    }

    # ── AI 聊天 API 代理 → api-proxy.js 端口 3721（SSE 流式支持）──
    location /api/chat {
        proxy_pass http://127.0.0.1:3721/api/chat;
        proxy_http_version 1.1;
        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 Connection "";
        proxy_buffering off;
        proxy_cache off;
        chunked_transfer_encoding on;
        proxy_read_timeout 120s;
        proxy_send_timeout 60s;
    }

    # ── 模型列表 API ──
    location /api/models {
        proxy_pass http://127.0.0.1:3721/api/models;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # ── Persona Studio API → 后端端口 3002 ──
    location /api/ps/ {
        add_header Access-Control-Allow-Origin * always;
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-API-Key" always;
        if ($request_method = OPTIONS) {
            return 204;
        }
        proxy_pass http://127.0.0.1:3002/api/ps/;
        proxy_http_version 1.1;
        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_read_timeout 120s;
    }

    # ── 后端健康检查 ──
    location /api/health {
        proxy_pass http://127.0.0.1:3721/api/health;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # ── GLADA 自主开发Agent API → 端口 3900 ──
    location /api/glada/ {
        proxy_pass http://127.0.0.1:3900/api/glada/;
        proxy_http_version 1.1;
        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_read_timeout 120s;
    }

    # ── 其他 API → Express 后端端口 3000 ──
    location /api/ {
        proxy_pass http://127.0.0.1:3000/api/;
        proxy_http_version 1.1;
        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_cache_bypass $http_upgrade;
    }

    # ── Persona Studio WebSocket → 预览进度推送 ──
    location /ws/preview {
        proxy_pass http://127.0.0.1:3002;
        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_read_timeout 86400;
    }

    # ── WebSocket → 看板实时推送端口 8080 ──
    location /ws {
        proxy_pass http://127.0.0.1:8080;
        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_read_timeout 86400;
    }

    # ── Persona Studio 前端静态文件 ──
    location /persona-studio/ {
        proxy_pass http://127.0.0.1:3002/persona-studio/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # ── DEV-004 之之 · 沙盒路由 ──
    location /DEV-004/ {
        alias /var/www/DEV-004/;
        index index.html;
        try_files $uri $uri/ =404;
    }

    # ── DEV-005 小草莓 · 沙盒路由 ──
    location /DEV-005/ {
        alias /var/www/DEV-005/;
        index index.html;
        try_files $uri $uri/ =404;
    }

    # ── 错误页面 ──
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}
