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

Socket.IO本地正常部署Nginx后WebSocket连接超时求助

问题求助:WebSocket连接失败与SSH隧道MongoDB连接错误

一、WebSocket连接失败问题

环境与现象

  • 服务器运行Node.js+Socket.IO应用,监听5000端口,使用NGINX作为反向代理处理WebSocket连接与API请求
  • 客户端尝试连接地址 ws://137.184.232.75:5000/socket.io/?EIO=4&transport=websocket 时失败,浏览器控制台报错:

WebSocket connection to 'ws://137.184.232.75:5000/socket.io/?EIO=4&transport=websocket' failed:
WebSocket connection error: tc: websocket error.

已完成的排查操作

  • 服务器状态验证:通过lsof -i :5000确认Node.js服务正常监听5000端口,服务本身支持WebSocket协议
  • 防火墙排查:已开放5000端口,临时关闭防火墙后问题仍存在
  • NGINX配置检查:已配置WebSocket反向代理支持,完整配置如下:
server {
    listen 80;
    server_name 137.184.232.75; 

    root /var/www/app.example.com/public_html/example/browser;
    index index.csr.html index.html index.htm;

    location / {
        try_files $uri $uri/ @node_server;
    }

    location @node_server {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /socket.io/ {
        proxy_pass http://localhost:5000/socket.io/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /api/ {
        proxy_pass http://localhost:5000/api/;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }

    location /api/users/uploads/ {
        alias /var/www/backend/userUploads/;
        autoindex on;
        access_log /var/log/nginx/uploads_access.log;
        error_log /var/log/nginx/uploads_error.log debug;
    }

    location ~* ^/assets/.*\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires max;
        log_not_found off;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location = /404.html {
        root /usr/share/nginx/html;
    }
}
  • 日志排查:服务器日志未记录WebSocket连接尝试时的错误信息
  • 客户端代码验证:客户端已配置正确的连接地址,代码如下:
    socket.config.ts
import { SocketIoConfig } from 'ngx-socket-io';

export const socketConfig: SocketIoConfig = {
  url: 'ws://137.184.232.75:5000/socket.io/',
  options: {
    path: '/socket.io',
    transports: ['websocket', 'polling'],
    autoConnect: true,
    reconnectionAttempts: 5,
    timeout: 60000,
  },
};

socket.service.ts

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { isPlatformBrowser } from '@angular/common';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class SocketService {
  constructor(
    private socket: Socket,
    @Inject(PLATFORM_ID) private platformId: Object
  ) {}

  connect() {
    if (isPlatformBrowser(this.platformId) && !this.socket.ioSocket.connected) {
      console.log('Attempting to connect to WebSocket...');
      this.socket.connect();

      this.socket.ioSocket.on('connect', () => {
        console.log('WebSocket connection established');
      });

      this.socket.ioSocket.on('connect_error', (err: any) => {
        console.error('WebSocket connection error:', err);
      });

      this.socket.ioSocket.on('disconnect', () => {
        console.log('WebSocket connection disconnected');
      });
    }
  }

  disconnect() {
    if (this.socket.ioSocket.connected) {
      console.log('Disconnecting WebSocket...');
      this.socket.disconnect();
    }
  }

  listen(eventName: string): Observable<any> {
    console.log(`Listening for event: ${eventName}`);
    return this.socket.fromEvent(eventName);
  }

  emit(eventName: string, data: any): void {
    console.log(`Emitting event: ${eventName} with data:`, data);
    this.socket.emit(eventName, data);
  }
}

二、SSH隧道连接MongoDB错误问题

  • 服务器通过SSH隧道连接远程MongoDB实例,已将私钥权限设置为chmod 600,但服务器日志持续报错**"Invalid username"**

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 17:21:00