解决 Dify HTTP 节点请求外网超时问题
DevOps
Dify Docker Proxy Network 现象描述
在本地 Docker 部署的 Dify 中,使用 HTTP 节点请求外网(如 Reddit RSS)时,经常报 Handshake operation timed out。这种报错通常伴随 10 秒以上的延迟,最终导致节点运行失败。
报错原因
这个问题涉及 Dify 内部的多层网络机制:
- 容器网络隔离:Docker 容器默认不走宿主机的系统代理。
- 沙箱隔离 (SSRF):Dify 强制要求 HTTP 节点流量通过一个专门的
ssrf_proxy(ubuntu/squid) 容器。即便修改了api容器的代理,沙箱依然无法联通外网。 - 内网劫持隐患:如果在配置代理时没有设定白名单,Dify 内部微服务(如
api到db或plugin_daemon)的通信会被代理拦截,导致页面报 502 或 500 错误。
修复步骤
1. 修改 .env 配置
打开 dify/docker/.env,找到 SSRF_PROXY 和 SANDBOX 配置项。将出站流量重定向到宿主机的代理端口(假设端口为 7890)。
# 修改 .env 中的沙箱出口地址
SANDBOX_HTTP_PROXY=[http://host.docker.internal:7890](http://host.docker.internal:7890)
SANDBOX_HTTPS_PROXY=[http://host.docker.internal:7890](http://host.docker.internal:7890)
SSRF_PROXY_HTTP_URL=[http://host.docker.internal:7890](http://host.docker.internal:7890)
SSRF_PROXY_HTTPS_URL=[http://host.docker.internal:7890](http://host.docker.internal:7890)
2. 更新 docker-compose.yaml
编辑 dify/docker/docker-compose.yaml,针对 api 和 worker 服务注入环境变量。
```YAML
api:
# ...
environment:
# 注入外网代理
HTTP_PROXY: [http://host.docker.internal:7890](http://host.docker.internal:7890)
HTTPS_PROXY: [http://host.docker.internal:7890](http://host.docker.internal:7890)
# 注入沙箱劫持配置
SSRF_PROXY_HTTP_URL: [http://host.docker.internal:7890](http://host.docker.internal:7890)
SSRF_PROXY_HTTPS_URL: [http://host.docker.internal:7890](http://host.docker.internal:7890)
# 关键:配置内网白名单,防止组件通信走代理导致 502
NO_PROXY: localhost,127.0.0.1,api,db,redis,worker,ssrf_proxy,sandbox,weaviate,plugin_daemon
注:worker 服务也需要同步添加上述配置。
3. 重启容器
必须彻底销毁旧容器以清理环境变量缓存。
docker-compose down
docker-compose up -d
验证方法
进入 API 容器执行网络测试:
执行 docker exec -it docker-api-1 /bin/bash
执行 env | grep -i proxy 确认变量已注入
执行 curl -I https://www.reddit.com 若返回 HTTP/2 200 或 301/302,说明链路正常
业务验证
在 Dify 前台的工作流中运行 HTTP 节点。注意在 Header 中配置常用的 User-Agent(如 Mozilla/5.0…)。如果节点在 3 秒内返回数据且状态为 SUCCESS,则修复完成。