AWS ECS Fargate中Traefik代理WSS服务出现502 Bad Gateway错误求助
问题排查与修复方案
1. 补充WebSocket服务协议配置
Traefik默认支持WebSocket转发,但需明确指定后端服务的协议类型,避免连接时出现EOF错误。给Container1添加服务关联及协议配置的Docker标签:
"traefik.http.routers.container1-router.service": "container1-service", "traefik.http.services.container1-service.loadbalancer.server.port": "1234", "traefik.http.services.container1-service.loadbalancer.server.scheme": "ws"
由于Traefik已在websecure入口处理TLS加密,后端服务使用ws协议即可,Traefik会自动将外部的WSS请求转为内部WS请求转发。
2. 排查StripPrefix中间件的影响
WebSocket连接依赖Upgrade请求头,StripPrefix中间件可能干扰请求路径或头信息。可先临时移除中间件测试:
// 注释或删除以下标签 // "traefik.http.middlewares.container1-strip.stripprefix.prefixes": "/container1", // "traefik.http.middlewares.container1-strip.stripprefix.forceSlash": "false", // "traefik.http.routers.container1-router.middlewares": "container1-strip"
若移除后WSS连接正常,需确认后端服务能处理去掉前缀后的路径(即根路径/),再重新启用中间件。
3. 验证任务内容器网络连通性
进入Traefik容器,执行命令测试与Container1的WebSocket连接:
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: test" http://localhost:1234
若返回HTTP/1.1 101 Switching Protocols,说明后端服务正常响应;若返回EOF或连接失败,需检查Container1的WSS服务是否正常监听1234端口,或服务是否启动异常。
4. 查看Traefik详细日志定位问题
已启用--log.level=DEBUG,重点关注以下日志内容:
- Traefik是否成功发现Container1的服务实例
- 转发请求到Container1时的连接建立细节
- 是否存在TLS握手或证书相关错误(即使已设置
insecureSkipVerify=true)
修复后的Container1 Docker标签示例
"dockerLabels": { "traefik.enable": "true", "traefik.http.routers.container1-router.tls": "true", "traefik.http.routers.container1-router.rule": "PathPrefix(`/container1`)", "traefik.http.routers.container1-router.entrypoints": "websecure", "traefik.http.routers.container1-router.service": "container1-service", "traefik.http.services.container1-service.loadbalancer.server.port": "1234", "traefik.http.services.container1-service.loadbalancer.server.scheme": "ws", "traefik.http.middlewares.container1-strip.stripprefix.prefixes": "/container1", "traefik.http.middlewares.container1-strip.stripprefix.forceSlash": "false", "traefik.http.routers.container1-router.middlewares": "container1-strip" }
内容的提问来源于stack exchange,提问作者JulienChamp
相关产品推荐
相关产品推荐

