Node.js Socket.io不适用于nginx和https
我有一个React应用程序与一个非常简单的节点后端通信,它在本地运行得非常好,在http上运行得非常好。出于显而易见的原因,该应用程序现在以https运行,但套接字不再连接。我一直在寻找这么多的线程,但找不到一个方法来修复它。下面是我的代码和nginx conf的套接字部分 客户端:Node.js Socket.io不适用于nginx和https,node.js,nginx,socket.io,Node.js,Nginx,Socket.io,我有一个React应用程序与一个非常简单的节点后端通信,它在本地运行得非常好,在http上运行得非常好。出于显而易见的原因,该应用程序现在以https运行,但套接字不再连接。我一直在寻找这么多的线程,但找不到一个方法来修复它。下面是我的代码和nginx conf的套接字部分 客户端: const socket = io('wss://sub.domain.io', { path: '/my-path',}); 服务器端: const server = require('http').creat
const socket = io('wss://sub.domain.io', { path: '/my-path',});
服务器端:
const server = require('http').createServer();
const io = require('socket.io')(server, {
path: '/my-path',
serveClient: false
});
server.listen(8080);
nginx代理
server {
....website serving part...
location /my-path/ {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
错误是:
GEThttps://sub.domain.io/my-path/?EIO=3&transport=polling&t=MsCot3e net::ERR_CONNECTION_TIMED_OUT VM17:1
,这看起来像是超时,因为它无法连接。所以我在这么多小时后找到了一个答案,它非常愚蠢和简单。我的Nginx只处理www网站(DNS重定向对于网站来说已经足够了),因此我需要www.sub.domain.io:
const socket = io('wss://www.sub.domain.io', { path: '/my-path',});
删除React标记,因为问题中没有React代码。