worker_processes auto;
events { worker_connections 1024; }

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  sendfile      on;
  keepalive_timeout 65;

  # 上游服务
  upstream backend {
    server server:8888;
  }

  upstream frontend {
    server frontend:3000;
  }

  # HTTPS 反代（将证书放在 /docker/nginx/ssl 下映射到 /etc/nginx/ssl）
  server {
    listen 8083 ssl http2;
    server_name _;

    ssl_certificate     /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    client_max_body_size 50m;

    # HTTP 请求到 HTTPS 端口时自动跳转
    error_page 497 =301 https://$host:$server_port$request_uri;

    # 指纹特征 - 用于 FOFA/Shodan 等搜索引擎识别
    add_header X-Powered-By "Xingrin ASM" always;

    location /api/ {
      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 300s;  # 5分钟，支持大数据量导出
      proxy_send_timeout 300s;
      proxy_pass http://backend;
    }

    # WebSocket 反代
    location /ws/ {
      proxy_pass http://backend;
      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;  # 24小时，防止 WebSocket 超时
    }

    # 前端反代
    location / {
      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_pass http://frontend;
    }
  }
}
