CMSPRO社区

AI代理应用出现SSE相关错误解决方法 回复

使用cmspro的代理应用,例如“AI聚合代理(AI号池)”等应用遇到长任务时会出现Stream error: SSE stream error: Transport error: error decoding response body (origin: downstream)的错误。

下面可以通过以下方法解决

  • PHP 运行方式:PHP-FPM(宝塔 FastCGI socket)
  • 关键点: SSE 流式代理应用(Clineproxy/Codegeex/Aiproxy 等)必须关闭 FastCGI 缓冲与 gzip ,否则下游客户端报 error decoding response body

Nginx 伪静态配置

  1. 核心 Laravel 重写规则(必须保留)
    location / {
     try_files $uri /index.php?$query_string;
    }
  2. SSE 代理端点专用规则(关键,优先级高于 location / )
  • 正则 location( ~ )优先级高于普通 location / ,会自动覆盖所有代理端点,不影响其他页面。
  • 如果还有新的应用没有在内可以使用|隔开加入到下面location ~内。

Linux服务器

# 覆盖所有 AI 代理 SSE 端点(clineproxy/codegeex/aiproxy/aihub/deepseek2api/opencode)
location ~ ^/api/(cline|codegeex|aiproxy|aihub|deepseek2api|opencode)/ {
    # 关闭 gzip,避免 SSE 流被压缩导致客户端解码失败
    gzip off;
    # 关闭 FastCGI 缓冲(关键!proxy_buffering 对 fastcgi 无效)
    fastcgi_buffering off;
    fastcgi_cache off;
    # 与代码 X-Accel-Buffering: no 双保险
    add_header X-Accel-Buffering no;
    # FastCGI 超时(长流需放宽)
    fastcgi_read_timeout 600s;
    fastcgi_send_timeout 600s;
    fastcgi_connect_timeout 15s;

    # 宝塔 fastcgi socket(按实际 PHP 版本调整)
    fastcgi_pass unix:/tmp/php-cgi-83.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    # 正则 location 需显式指定 SCRIPT_FILENAME
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}

Windows服务器

# 覆盖所有 AI 代理 SSE 端点(clineproxy/codegeex/aiproxy/aihub/deepseek2api/opencode)
location ~ ^/api/(cline|codegeex|aiproxy|aihub|deepseek2api|opencode)/ {
  # 关闭 gzip,避免 SSE 流被压缩导致客户端解码失败
  gzip off;
  # 关闭 FastCGI 缓冲(关键!proxy_buffering 对 fastcgi 无效)
  fastcgi_buffering off;
  fastcgi_cache off;
  # 与代码 X‑Accel‑Buffering: no 双保险
  add_header X‑Accel‑Buffering no;
  # FastCGI 超时(长流SSE需放宽)
  fastcgi_read_timeout 600s;
  fastcgi_send_timeout 600s;
  fastcgi_connect_timeout 15s;

  # ========= Windows重点:改用TCP端口,不要unix sock =========
    # 端口打开 D:\BtSoft\nginx\conf\php\83.conf 找到 fastcgi_pass 查看
  fastcgi_pass 127.0.0.1:xxx;

  fastcgi_index index.php;
  include fastcgi.conf;
  # 正则location必须显式指定入口脚本
  fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
  1. 安全加固(建议保留)
# 禁止访问隐藏文件(.env、.git 等),允许 .well-known
location ~ /\.(?!well-known).* {
    deny all;
}

# 上传目录禁止执行 PHP
location ~* ^/Uploads/.*\.php$ {
    deny all;
}
  1. 静态资源缓存(可选)
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
     expires 30d;
     add_header Cache-Control "public, immutable";
     access_log off;
    }

    PHP 设置清单(php.ini / PHP-FPM 池配置)

  2. SSE 流式必需(关键)
    打开PHP配置,在php.ini配置中进行以下调整
    ; 关闭输出压缩(SSE 必须,否则流被压缩)
    zlib.output_compression = Off
    ; 关闭输出缓冲(SSE 必须,否则小块数据被缓冲)
    output_buffering = Off
    ; 开启隐式刷新
    implicit_flush = On
    ; 取消执行时间限制(长流)
    max_execution_time = 0
  3. 建议配置
; 内存限制(长流/大模型响应)
memory_limit = 512M
; 上传大小(按需)
upload_max_filesize = 50M
post_max_size = 50M

常见错误对照表

错误现象 原因 解决方案
error decoding response body (origin: downstream) Nginx gzip 压缩了 SSE 流 gzip off
同上 FastCGI 缓冲未关闭 fastcgi_buffering off (不是 proxy_buffering)
The GET method is not supported... Supported methods: POST 用 GET 测试 POST 接口 改用 POST 请求
SSE 中途截断/内容不完整 上游 JSON 含未转义换行符 开启 debug 日志定位,上游统一转义
长流被掐断 fastcgi_read_timeout 过短 设为 600s
CMSPRO官方工作人员账号!

全部评论 2

  • 体验官
    体验官 广东·佛山 #2

    关闭 Web 服务器缓冲(最优先,体感影响最大)
    Windows + Linux(Nginx)
    文件:站点 PHP location 配置(宝塔为 D:\BtSoft\nginx\conf\php{版本}.conf,Linux 为 /www/server/nginx/conf/... 或站点 conf)

    操作:在 location ~ .php(.*)$ { ... } 内、fastcgi_pass 之后加入:

    fastcgi_buffering off;
    fastcgi_request_buffering off;
    gzip off;

    Linux(Apache,若用 mod_php / php-fpm)
    文件:站点 .htaccess 或 vhost 配置

    # 关闭输出缓冲(mod_php)
    php_flag output_buffering Off
    # 关闭 gzip 对 SSE 的压缩
    SetEnvIfNoCase Content-Type "text/event-stream" no-gzip dont-vary
  • 官方发言人
    官方发言人 楼主 广东·佛山 #1

    如果还是没有解决,需要修改PHP的fpm配置

    • ① 找到 PHP-FPM 池配置
      通常在 /www/server/php/83/etc/php-fpm.conf 或 /www/server/php/83/etc/php-fpm.d/www.conf 。
      grep -rn "request_terminate_timeout" /www/server/php/83/etc/
    • ② 修改为 0(不限制)或调大
      设为 0 表示不限制。SSE 长流场景建议设为 0 ,或至少 600 (10分钟)以上。
      ; 在 www 池配置中
      request_terminate_timeout = 0
    • ③ 重启 PHP

    同时建议检查(避免其他限制)

    request_slowlog_timeout = 0
    max_execution_time = 0
请先 登录 后再回复