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 伪静态配置
- 核心 Laravel 重写规则(必须保留)
location / { try_files $uri /index.php?$query_string; } - SSE 代理端点专用规则(关键,优先级高于 location / )
-
正则 location( ~ )优先级高于普通 location / ,会自动覆盖所有代理端点,不影响其他页面。
-
如果还有新的应用没有在内可以使用|隔开加入到下面
location ~内。# 覆盖所有 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; }
- 安全加固(建议保留)
# 禁止访问隐藏文件(.env、.git 等),允许 .well-known
location ~ /\.(?!well-known).* {
deny all;
}
# 上传目录禁止执行 PHP
location ~* ^/Uploads/.*\.php$ {
deny all;
}
- 静态资源缓存(可选)
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 池配置)
- SSE 流式必需(关键)
打开PHP配置,在php.ini配置中进行以下调整; 关闭输出压缩(SSE 必须,否则流被压缩) zlib.output_compression = Off ; 关闭输出缓冲(SSE 必须,否则小块数据被缓冲) output_buffering = Off ; 开启隐式刷新 implicit_flush = On ; 取消执行时间限制(长流) max_execution_time = 0 - 建议配置
; 内存限制(长流/大模型响应)
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 |