OpenWebUI 訪問後端 LLM 報錯:Unexpected token ‘d’, “data: {“id”… is not valid JSON

這幾天在部署企業內部 AI, Open WebUI 用 Docker 部署在一台 Web Server 上,因為上面有多個服務,所以用了 nginx 反向代理,真正運行的端口在 3000,反向代理到 HTTPS。當我用 Open Webui 訪問 Inference Server上部署的後端 LLM 的時候,不管後端用什麼引擎,Open Webui都報錯:

Unexpected token 'd', "data: {"id"... is not valid JSON

一開始以為是後端 LLM 模型的設置問題,後來又以為是 Open WebUI 的設置問題。

折騰了挺久才知道其實是 web socket 設置的問題。


這個錯誤訊息的關鍵是:回來的串流內容長得像 SSE(每行前面有 data: …),但 Open WebUI 這邊把它當成「一次性 JSON」去 JSON.parse(),所以第一個字符是 d 就直接報錯了。

最後解決方法分兩部分,涉及 nginx 設置,也涉及 Open WebUI 的 docker compose 設置。

Docker Compose 文件裡面,需要確保如下環境變量:

environment:
  - WEBUI_URL=https://chat.example.com
  - CORS_ALLOW_ORIGIN=https://chat.example.com
  - WEBUI_SESSION_COOKIE_SECURE=true
  - WEBUI_COOKIE_SECURE=True

    Nginx 的設置裡面:

    upstream open-webui {
        keepalive 32;
        server localhost:3000;
    }
    server {
        server_name   chat.augwit.com;
    
        access_log /var/log/nginx/chat.example.com/access.log;
        error_log /var/log/nginx/chat.example.com/error.log;
    
    
        location /ws/ {
          proxy_pass http://open-webui;
          proxy_http_version 1.1;
    
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
    
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Real-IP $remote_addr;
    
          proxy_buffering off;
          proxy_read_timeout 3600;
          proxy_send_timeout 3600;
        }
    
        location / {
            proxy_pass   http://open-webui;
    
            proxy_http_version 1.1;
    
            # WebSocket 必需
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
    
            proxy_set_header Host $host;
            proxy_set_header X-NginX-Proxy true;
            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_set_header X-Forwarded-Port $server_port;
            proxy_set_header X-Forwarded-Uri $request_uri;
    
            # 串流 / SSE 常需要關掉 buffering,不然容易卡或解析怪
            proxy_buffering off;
    
            # 依需求調大超時(串流聊天很容易超過預設)
            proxy_read_timeout 3600;
            proxy_send_timeout 3600;
    
            client_max_body_size  1024m;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/chat.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/chat.example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }

    最近因為在不同語言環境下工作,所以這篇正好是中文繁體。