# ═══════════════════════════════════════════════════════════
# 零点原核 · 冰朔个人频道 Nginx 配置
# ═══════════════════════════════════════════════════════════
#
# 编号: ZY-SVR-NGX-YAOMING
# 域名: guanghuyaoming.com (ZY_FACE_DOMAIN)
# 服务器: 43.134.16.246 (ZY-SVR-002 · 新加坡)
# 守护: 铸渊 · ICE-GL-ZY001
# 版权: 国作登字-2026-A-00037559
#
# 架构:
#   guanghuyaoming.com → 冰朔零点原核频道
#   前端: /opt/zhuyuan/sites/yaoming/ (静态HTML)
#   后端: 复用铸渊主权服务器 (端口3800)
#   AI: 国内模型网关 + 第三方代理模型
#
# 域名变量由 GitHub Secrets 注入:
#   ZY_FACE_DOMAIN — 零点原核域名
# ═══════════════════════════════════════════════════════════

server {
    listen 80;
    server_name ZY_FACE_DOMAIN_PLACEHOLDER;

    # ─── 安全头 ───
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Server-Identity "ZY-SVR-002" always;
    add_header X-Site-Mode "yaoming-channel" always;

    # ─── 静态文件根目录 · 零点原核频道 ───
    root /opt/zhuyuan/sites/yaoming;
    index index.html;

    # ─── 前端静态文件 ───
    location / {
        try_files $uri $uri/ /index.html;
    }

    # ─── 铸渊核心 API (端口 3800) ───
    location /api/ {
        proxy_pass http://127.0.0.1:3800;
        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;
        proxy_set_header X-Site-Mode "yaoming-channel";
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 120s;
    }

    # ─── AI 聊天 API (含国内模型网关) ───
    # /api/chat         → 第三方模型线路
    # /api/chat/domestic → 国内四模型智能路由
    location /api/chat {
        proxy_pass http://127.0.0.1:3800;
        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 Connection "";
        proxy_buffering off;
        proxy_cache off;
        chunked_transfer_encoding on;
        proxy_read_timeout 120s;
        proxy_send_timeout 60s;
    }

    # ─── MCP Server 反向代理 (端口 3100) ───
    location /api/mcp/ {
        proxy_pass http://127.0.0.1:3100/;
        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 Authorization $http_authorization;
        proxy_connect_timeout 10s;
        proxy_read_timeout 120s;

        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" always;
        if ($request_method = OPTIONS) { return 204; }
    }

    # ─── GitHub OAuth 回调 ───
    location /api/auth/ {
        proxy_pass http://127.0.0.1:3800;
        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;
    }

    # ─── WebSocket ───
    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_read_timeout 86400;
    }

    # ─── 健康探针 ───
    location = /health {
        proxy_pass http://127.0.0.1:3800/api/health;
        proxy_set_header Host $host;
    }

    # ─── 静态资源缓存 ───
    location /static/ {
        alias /opt/zhuyuan/sites/yaoming/static/;
        expires 7d;
        add_header Cache-Control "public, immutable";
    }

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

    # ─── 访问日志 ───
    access_log /opt/zhuyuan/data/logs/nginx-yaoming.log;
    error_log /opt/zhuyuan/data/logs/nginx-yaoming-error.log;
}
