#user  nobody;
worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # 限制请求大小（支持大文件上传）
      client_max_body_size 200M;
    # client_body_buffer_size 128k;

    # ============================================================
    # HTTP -> HTTPS 重定向（所有域名）
    # ============================================================
    server {
        listen       80;
        server_name  www.douxue.net.cn
                     index.douxue.net.cn
                     h5.douxue.net.cn
                     api.douxue.net.cn
                     up.douxue.net.cn
                     bot.douxue.net.cn
                     douxue.net.cn;

        access_log  logs/access_redirect.log;
        error_log   logs/error_redirect.log;

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

    # ============================================================
    # 公司官网 www.douxue.net.cn
    # 暂留空，后续单独开发
    # ============================================================
    server {
        listen       443 ssl;
        server_name  www.douxue.net.cn douxue.net.cn;

        access_log  logs/access_www.log;
        error_log   logs/error_www.log;

        # SSL 配置（使用泛域名证书）
        # 兼容 OpenSSL 1.0.2 + 现代浏览器
        ssl_certificate      C:/nginx-1.29.1/ssl/douxue.net.cn.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/douxue.net.cn.key;
        ssl_protocols TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # 官网静态文件目录（暂留空）
        root   c:/www/website;
        index  index.html;

        # 官网静态资源缓存
        location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|webp|ttf|woff2?)$ {
            try_files $uri =404;
            expires 7d;
            access_log off;
        }

        # SPA 路由支持
        location / {
            try_files $uri $uri/ /index.html;
        }

        # 错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # BS端应用 index.douxue.net.cn
    # 静态文件目录：c:/www/dxAifront
    # ============================================================
    server {
        listen       443 ssl;
        server_name  index.douxue.net.cn;

        access_log  logs/access_index.log;
        error_log   logs/error_index.log;

        # SSL 配置（独立证书）
        ssl_certificate      C:/nginx-1.29.1/ssl/index.douxue.net.cn.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/index.douxue.net.cn.key;
        ssl_protocols TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # BS端静态文件目录
        root   c:/www/dxAifront;
        index  index.html;

        # 上传文件访问
        location /files/ {
            alias c:/www/dxAifront/files/;
            expires 30d;
            add_header Cache-Control "public, immutable";
        }

        # 静态资源缓存（JS/CSS/图片等）
        location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|webp|ttf|woff2?)$ {
            try_files $uri =404;
            expires 30d;
            add_header Cache-Control "public, immutable";
            access_log off;
        }

        # API代理 - 转发到本地后端
        location /api/ {
            proxy_pass http://127.0.0.1:3000/api/;
            proxy_http_version 1.1;
            proxy_set_header Host api.douxue.net.cn;
            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-Forwarded-Host $host;

            # 超时设置
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;

            # 允许跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

            if ($request_method = 'OPTIONS') {
                return 204;
            }
        }

        # WebSocket代理
        location /socket.io/ {
            proxy_pass http://127.0.0.1:3000/socket.io/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host api.douxue.net.cn;
            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;
        }

        # SPA 路由支持
        location / {
            try_files $uri $uri/ /index.html;
        }

        # 禁止缓存 HTML
        location = /index.html {
            add_header Cache-Control "no-cache, no-store, must-revalidate";
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # 手机端H5 h5.douxue.net.cn
    # 静态文件目录：c:/www/h5
    # ============================================================
    server {
        listen       443 ssl;
        server_name  h5.douxue.net.cn;

        access_log  logs/access_h5.log;
        error_log   logs/error_h5.log;

        # SSL 配置（独立证书）
        ssl_certificate      C:/nginx-1.29.1/ssl/h5.douxue.net.cn.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/h5.douxue.net.cn.key;
        ssl_protocols TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # H5静态文件目录
        root   c:/www/h5;
        index  index.html;

        # 上传文件访问
        location /files/ {
            alias c:/www/dxAifront/files/;
            expires 30d;
            add_header Cache-Control "public, immutable";
        }

        # H5静态资源缓存（JS/CSS/图片等）
        location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|webp|ttf|woff2?)$ {
            try_files $uri =404;
            expires 30d;
            add_header Cache-Control "public, immutable";
            access_log off;
        }

        # API代理 - 转发到本地后端
        location /api/ {
            proxy_pass http://127.0.0.1:3000/api/;
            proxy_http_version 1.1;
            proxy_set_header Host api.douxue.net.cn;
            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-Forwarded-Host $host;

            # 超时设置
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;

            # 允许跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

            if ($request_method = 'OPTIONS') {
                return 204;
            }
        }

        # WebSocket代理
        location /socket.io/ {
            proxy_pass http://127.0.0.1:3000/socket.io/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host api.douxue.net.cn;
            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;
        }

        # SPA 路由支持
        location / {
            try_files $uri $uri/ /index.html;
        }

        # 禁止缓存 HTML
        location = /index.html {
            add_header Cache-Control "no-cache, no-store, must-revalidate";
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # API接口 api.douxue.net.cn
    # 代理到本地VPS后端 (127.0.0.1:3000)
    # ============================================================
    server {
        listen       443 ssl;
        server_name  api.douxue.net.cn;

        access_log  logs/access_api.log;
        error_log   logs/error_api.log;

        # SSL 配置（独立证书）
        ssl_certificate      C:/nginx-1.29.1/ssl/api.douxue.net.cn.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/api.douxue.net.cn.key;
        ssl_protocols TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # 健康检查
        location /health {
            access_log off;
            return 200 "api healthy\n";
            add_header Content-Type text/plain;
        }

        # WebSocket 代理（Socket.IO）
        location /socket.io/ {
            proxy_pass http://127.0.0.1:3000/socket.io/;
            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_read_timeout 86400;
        }

        # API代理 - 转发到本地后端 127.0.0.1:3000
        location / {
            # 文件上传大小限制 - 移除限制
            # client_max_body_size 1G;
            # client_body_buffer_size 1M;
            
            proxy_pass http://127.0.0.1:3000/;
            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 X-Forwarded-Host $host;

            # 超时设置 - 音色克隆需要更长时间
            proxy_connect_timeout 180s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;

            # 允许跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

            if ($request_method = 'OPTIONS') {
                return 204;
            }
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # 文件上传API up.douxue.net.cn
    # 代理到本地VPS后端 (127.0.0.1:3000)
    # 支持WebSocket (Socket.IO)
    # ============================================================
    server {
        listen       443 ssl;
        server_name  up.douxue.net.cn;

        access_log  logs/access_up.log;
        error_log   logs/error_up.log;

        # SSL 配置（独立证书 up.douxue.net.cn）
        ssl_certificate      c:/nginx-1.29.1/ssl/up.douxue.net.cn_chain.pem;
        ssl_certificate_key  c:/nginx-1.29.1/ssl/up.douxue.net.cn_key.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # 大文件上传支持
        client_max_body_size 500M;

        # 健康检查
        location /health {
            access_log off;
            return 200 "up api healthy\n";
            add_header Content-Type text/plain;
        }

        # WebSocket 代理（Socket.IO）- 上传进度推送
        location /socket.io/ {
            proxy_pass http://127.0.0.1:3000/socket.io/;
            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_read_timeout 86400;
            proxy_send_timeout 86400;
        }

        # API代理 - 转发到本地VPS后端
        location / {
            proxy_pass http://127.0.0.1:3000;
            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 X-Forwarded-Host $host;

            # 超时设置（大文件上传需要更长超时）
            proxy_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;

            # 允许跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

            if ($request_method = 'OPTIONS') {
                return 204;
            }
        }

        # 文件访问 (可选：配置OSS直链或本地文件服务)
        # location /files/ {
        #     alias c:/www/files/;
        #     expires 30d;
        # }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # FRP管理面板 frp.douxue.net.cn
    # 代理到本地 127.0.0.1:7500
    # ============================================================
    server {
        listen       443 ssl;
        server_name  frp.douxue.net.cn;

        access_log  logs/access_frp.log;
        error_log   logs/error_frp.log;

        # SSL 配置（使用 frp 域名证书）
        ssl_certificate      C:/nginx-1.29.1/ssl/frp.douxue.net.cn_chain.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/frp.douxue.net.cn_key.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # FRP Dashboard 代理
        location / {
            proxy_pass http://127.0.0.1:7500/;
            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 X-Forwarded-Host $host;

            # 超时设置
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
        }

        # WebSocket 支持（如果FRP Dashboard需要）
        location /ws/ {
            proxy_pass http://127.0.0.1:7500/ws/;
            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;
        }

        # 健康检查
        location /health {
            access_log off;
            return 200 "frp dashboard healthy\n";
            add_header Content-Type text/plain;
        }
    }

    # ============================================================
    # 机器人控制服务 bot.douxue.net.cn
    # 代理到本地 bot-service (127.0.0.1:8765)
    # ============================================================
    server {
        listen       443 ssl;
        server_name  bot.douxue.net.cn;

        access_log  logs/access_robot.log;
        error_log   logs/error_robot.log;

        # SSL 配置（使用独立证书 bot.douxue.net.cn）
        ssl_certificate      C:/nginx-1.29.1/ssl/bot.douxue.net.cn_chain.pem;
        ssl_certificate_key  C:/nginx-1.29.1/ssl/bot.douxue.net.cn_key.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # H5 静态页面
        location /h5/ {
            alias c:/app/robot-service/h5/;
            index index.html;
            expires -1;
            add_header Cache-Control "no-cache, no-store, must-revalidate";
        }

        # 默认访问 index.html（SPA 模式）
        location = / {
            return 302 /h5/;
        }

        # 主业务登录与用户 API（须长于 /api/，避免被转到 robot-service:8765）
        location /api/auth/ {
            proxy_pass http://127.0.0.1:3000/api/auth/;
            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_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, DELETE, OPTIONS';
            add_header Access-Control-Allow-Headers 'Content-Type, Authorization';
        }

        # HTTP API 代理（机器人服务）
        location /api/ {
            proxy_pass http://127.0.0.1:8765;
            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_connect_timeout 60s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;

            # 允许跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, DELETE, OPTIONS';
            add_header Access-Control-Allow-Headers 'Content-Type, Authorization';
        }

        # WebSocket 代理（/ws/ 路径）
        location /ws/ {
            proxy_pass http://127.0.0.1:8765/ws/;
            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_read_timeout 86400;
            proxy_send_timeout 86400;
        }

        # 健康检查
        location /health {
            access_log off;
            proxy_pass http://127.0.0.1:8765/health;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # ============================================================
    # 调试服务 - 配置和日志访问
    # n.douxue.net.cn
    # ============================================================
    server {
        listen 443 ssl;
        server_name n.douxue.net.cn;

        access_log logs/access_debug.log;
        error_log logs/error_debug.log;

        # SSL 配置（独立证书 n.douxue.net.cn）
        ssl_certificate      c:/nginx-1.29.1/ssl/n.douxue.net.cn_chain.pem;
        ssl_certificate_key  c:/nginx-1.29.1/ssl/n.douxue.net.cn_key.key;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5:!3DES;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # Nginx根目录
        location / {
            root c:/nginx-1.29.1;
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
        }

        # Nginx日志目录
        location /logs/ {
            alias c:/nginx-1.29.1/logs/;
            autoindex on;
            default_type text/plain;
            charset utf-8;
        }

        # Nginx配置目录
        location /conf/ {
            alias c:/nginx-1.29.1/conf/;
            autoindex on;
            # 隐藏敏感文件
            location ~ /\. {
                deny all;
            }
        }

        # 后端目录
        location /backend/ {
            alias c:/app/dxai-backend/;
            autoindex on;
            default_type text/plain;
            charset utf-8;
        }

        # PM2日志
        location /pm2/ {
            alias C:/Users/Administrator/.pm2/logs/;
            autoindex on;
            default_type text/plain;
            charset utf-8;
        }

        # 健康检查
        location /health {
            access_log off;
            return 200 "debug server healthy\n";
            add_header Content-Type text/plain;
        }
    }
}
