# ═══════════════════════════════════════════════════════════
# Nginx 配置 · 光湖 FTCHAT (微调模型内测) · guanghu.online
# ═══════════════════════════════════════════════════════════
#
# 项目编号: ZY-PROJ-FTCHAT
# 服务器:   ZY-SVR-006 (43.153.203.105 · 新加坡)
# 守护:     铸渊 · ICE-GL-ZY001
# 版权:     国作登字-2026-A-00037559
#
# 安装位置: /etc/nginx/sites-available/ftchat-guanghu-online.conf
# 软链接:   /etc/nginx/sites-enabled/ftchat-guanghu-online.conf
#
# 替代旧的 zhiku-guanghu-online.conf (智库节点已下架, 见 archive/)
# ═══════════════════════════════════════════════════════════

# HTTP → HTTPS 重定向
server {
    listen 80;
    listen [::]:80;
    server_name guanghu.online www.guanghu.online;

    # Let's Encrypt ACME 验证
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

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

# HTTPS 主配置
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name guanghu.online www.guanghu.online;

    # SSL 证书 (Let's Encrypt, 已存在自旧 zhiku 部署)
    ssl_certificate     /etc/letsencrypt/live/guanghu.online/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/guanghu.online/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # 安全头
    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 Referrer-Policy "strict-origin-when-cross-origin" always;

    # 日志
    access_log /var/log/nginx/ftchat.access.log;
    error_log  /var/log/nginx/ftchat.error.log;

    # 静态前端根
    root /opt/guanghu/ftchat/public;
    index index.html;

    # 客户端体积上限 (避免大上下文 messages[] 爆掉)
    client_max_body_size 4m;

    # ─── HLI 接口反向代理 → Node.js 127.0.0.1:3010 ───
    location /hli/ {
        proxy_pass http://127.0.0.1:3010;
        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;

        # SSE 流式必须: 关闭缓冲, 长连接, 长超时
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 180s;
        proxy_send_timeout 180s;
        proxy_connect_timeout 10s;
        chunked_transfer_encoding on;
    }

    # /health 也直通后端 (CI 健康检查)
    location = /health {
        proxy_pass http://127.0.0.1:3010;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    # 静态资源缓存
    location ~* \.(?:js|css|woff2?|ttf|otf|png|jpg|jpeg|gif|webp|svg|ico)$ {
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
        try_files $uri =404;
    }

    # SPA fallback: 所有非 API、非静态文件都返回 index.html
    # index.html 强制走重新校验, 保证新版部署的 ?v= 哈希立刻生效
    location / {
        add_header Cache-Control "no-cache, must-revalidate" always;
        try_files $uri $uri/ /index.html;
    }

    # 禁止访问隐藏文件
    location ~ /\. {
        deny all;
        return 404;
    }
}
