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

在Azure Web App部署Node.js Socket.io服务器启动失败问题排查

问题排查:Azure Web App上Socket.io服务器启动失败且无响应

环境与代码说明

  • 部署环境:Azure Web App Linux Node.js 20 LTS(B1计划),已在应用配置中设置WEBSITES_PORT=8080
  • Socket.io服务器核心配置代码:
const io = new Server(socketServer, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"],
    credentials: false,
  },
  transports: ["polling", "websocket"],
  pingTimeout: 5000,
  pingInterval: 5000,
});

关键日志信息

服务器启动日志显示服务已启动,但容器因未响应HTTP ping请求被终止:

2024-12-13T14:04:29.440177474Z WebSocket server for CHAT is running on port 8080
2024-12-13T14:08:37.931Z ERROR - Container MYAPP-chat_0_cd509984 for site MYAPP-chat did not start within expected time limit. Elapsed time = 258.2538017 sec
2024-12-13T14:08:37.944Z ERROR - Container MYAPP-chat_0_cd509984 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.

排查与修复建议

  • 确保服务器监听正确地址:Socket.io绑定的HTTP服务器必须监听0.0.0.0而非localhost,否则Azure无法访问容器内服务。示例修正代码:
    const http = require('http');
    const socketServer = http.createServer((req, res) => {
      // 添加健康检查端点,响应Azure的预热请求
      if (req.url === '/') {
        res.writeHead(200);
        res.end('OK');
      }
    });
    const io = new Server(socketServer, { /* 你的配置 */ });
    
    socketServer.listen(process.env.PORT || 8080, '0.0.0.0', () => {
      console.log(`WebSocket server running on port ${process.env.PORT || 8080}`);
    });
    
  • 添加健康检查端点:Azure Web App的预热请求默认访问根路径/,Socket.io本身没有该路由,需手动添加简单HTTP响应确保容器能被探测到
  • 验证端口监听状态:通过Kudu控制台SSH连接到容器,执行netstat -tulpn | grep 8080确认端口是否被正确监听
  • 调整启动超时限制:在应用配置中增加WEBSITES_CONTAINER_START_TIME_LIMIT值(如300秒),给服务器足够的初始化时间
  • 检查环境变量读取:确保代码中使用process.env.PORT获取端口,而非硬编码8080,避免与Azure设置的环境变量冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:04:57