【问题标题】:Socket.io not working with Nginx reverse proxy in front of Node.jsSocket.io 不能在 Node.js 前面使用 Nginx 反向代理
【发布时间】:2015-12-14 20:31:15
【问题描述】:

我的节点应用程序在端口 3000 上运行。 然后我使用 nginx 作为反向代理,通过我的域的 SSL 为应用程序提供服务:

server {
        listen 443 ssl;

        ssl_certificate /etc/nginx/ssl/server.chain.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;

        server_name example.com;

        location / {
                proxy_pass http://localhost:3000;
                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;
    }
}

我用这一行在客户端初始化我的套接字:

var socket = io.connect('https://example.com/');

在服务器端使用这一行:

var io = socket_io()
app.io = io

但是当我启动我的应用程序时,我遇到了这个错误:

WebSocket connection to 'wss://example.com/socket.io/?EIO=3&transport=websocket&sid=jVNRhDOYJD20l9U-AAAA' failed: Error during WebSocket handshake: Unexpected response code: 502

你知道如何解决这个错误吗?

【问题讨论】:

    标签: node.js nginx websocket socket.io


    【解决方案1】:

    本文描述了在 Kaazing WebSocket 服务器前使用 nginx 作为 rev-proxy。

    https://kaazing.com/kaazing-and-nginx-the-best-of-both-worlds/

    文章中描述的配置可能与使用 NodeJS 作为服务器非常相似。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-27
      • 2021-04-26
      • 2021-07-01
      • 2021-12-01
      • 1970-01-01
      • 2011-10-09
      • 2021-09-15
      • 1970-01-01
      相关资源
      最近更新 更多