server{
    listen 80;
    server_name  localhost;
    client_max_body_size 10200M;
    root /usr/share/nginx/html/public;
    index  index.html index.htm;

    proxy_intercept_errors on;
    fastcgi_intercept_errors on;

    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-Client-Verify  SUCCESS;
              proxy_set_header    X-Client-DN      $ssl_client_s_dn;
              proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
              proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
              proxy_read_timeout 18000;
              proxy_connect_timeout 18000;
              proxy_send_timeout      18000;
              client_max_body_size 10200M;
             try_files $uri $uri/ /index.html;


                add_header Access-Control-Allow-Origin $http_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;
                 }
             }

    location /api/ {
        proxy_pass http://gdt-auto-in-svc/;
        proxy_read_timeout 18000;
        proxy_connect_timeout 18000;
        proxy_send_timeout      18000;
        client_max_body_size 10200M;
    }
    location /mcp {
        proxy_pass http://gdt-auto-mcp-in-svc; # 替换为你的 MCP 服务地址

        # 1. 禁用响应缓冲 (这是最关键的一步)
        proxy_buffering off;
        proxy_cache off;

        # 如果 Nginx 前面是 HTTPS，需要告诉后端这是安全的
        proxy_set_header X-Forwarded-Proto $scheme;

        # 2. 设置 HTTP 版本和连接头
        # SSE 需要 HTTP/1.1，并清除 Connection 头以防止被中途关闭
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        # 3. 延长超时时间
        # MCP 连接通常是长连接，防止 NGINX 在 60 秒内关闭“闲置”连接
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;

        # 4. 禁用分块传输编码的潜在干扰 (可选，视客户端而定)
        chunked_transfer_encoding off;

        # 5. 透传必要的头部 (增强兼容性)
        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 Accept "text/event-stream";

        # 增加：WebSocket/SSE 支持
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    
        # 增加：防止 502 错误
        proxy_connect_timeout 60s;
    }
    error_page  405     =200 $uri;
}
