You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure容器应用中WebSocket服务握手错误排查求助

解决Azure Container Apps中WebSocket服务持续出现握手失败日志的问题

你遇到的这个EOFError握手失败日志,几乎可以确定是Azure Container Apps的健康探测机制导致的——本地运行没有这个问题,是因为本地没有类似的自动HTTP探测请求,而Azure会定期向你的容器发送HTTP请求来检查服务健康状态,但你的WebSocket服务只处理WebSocket握手请求(带Upgrade: websocket头的HTTP请求),无法识别普通的HTTP GET请求,最终导致连接提前关闭,触发日志里的错误。

下面是具体的调整方向:

1. 修正健康探测配置

这是最核心的解决办法,有两种可选方案:

  • 方案一:添加健康检查端点
    在你的WebSocket服务里新增一个普通HTTP端点(比如/health),当收到GET请求时返回200状态码。然后在Azure Container Apps的健康探测设置中,将探测路径改为/health,协议选择HTTP。这样Azure的探测请求会被正常处理,不会再触发握手失败错误。
  • 方案二:改用TCP探测
    如果不想修改服务代码,可以直接把健康探测类型改成TCP。TCP探测只会检查容器端口是否处于开放状态,不会发送任何HTTP请求,完全不会干扰WebSocket服务的正常运行。

2. 确认WebSocket路由配置

检查Azure Container Apps的Ingress设置,确保已经启用了WebSocket支持(Azure默认是开启的,但可以确认一下),避免路由层错误地将WebSocket请求当作普通HTTP请求处理,引发额外的异常。

3. 排查额外流量(可选)

如果修改健康探测后日志仍有错误,可以在服务中添加日志记录,打印所有 incoming 请求的头部信息,确认是否有其他自动请求(比如监控、爬虫)打到服务上。比如在Python的websockets服务中,握手阶段可以先打印请求头:

async def handle_connection(websocket):
    # 打印请求头排查
    print(f"Incoming request headers: {websocket.request_headers}")
    # 后续业务逻辑...

附你提供的错误日志:

opening handshake failed
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 134, in parse
    request_line = yield from parse_line(read_line)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 380, in parse_line
    line = yield from read_line(MAX_LINE_LENGTH)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/streams.py", line 46, in read_line
    raise EOFError(f"stream ends after {p} bytes, before end of line")
EOFError: stream ends after 0 bytes, before end of line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/websockets/asyncio/server.py", line 353, in conn_handler
    await connection.handshake(
  File "/usr/local/lib/python3.12/site-packages/websockets/asyncio/server.py", line 204, in handshake
    raise self.protocol.handshake_exc
  File "/usr/local/lib/python3.12/site-packages/websockets/server.py", line 551, in parse
    request = yield from Request.parse(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/websockets/http11.py", line 136, in parse
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

内容的提问来源于stack exchange,提问作者MrCaracara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 07:08:17