简体   繁体   English

Socket.io无法与Node.js之前的Nginx反向代理一起使用

[英]Socket.io not working with Nginx reverse proxy in front of Node.js

I have my node app running on port 3000. Then I use nginx as a reverse proxy to serve the application with SSL of my domain : 我的节点应用程序在端口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;
    }
}

I initialize my socket in client-side with this line : 我在客户端用以下行初始化套接字:

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

And in servers-side with this line : 而在服务器端的这一行:

var io = socket_io()
app.io = io

But when I launch my application I have this error : 但是,当我启动我的应用程序时,会出现以下错误:

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

Do you know how to fix this error ? 您知道如何解决此错误吗?

This article describes using nginx as a rev-proxy in front of the Kaazing WebSocket server. 本文介绍了在Kaazing WebSocket服务器之前使用nginx作为rev-proxy。

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

The config described in the article might be very similar to using NodeJS as the server. 本文中描述的配置可能与使用NodeJS作为服务器非常相似。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM