Socket.io不适用于nginx和https
我有一个React应用程序与一个非常简单的节点后端进行通信,它在本地工作得很好,并且在http上工作得很好。出于明显的原因,该应用程序现在在https中运行,但套接字不再连接。我一直在寻找这么多线程,但是找不到解决它的方法。这是我的代码和nginx conf的套接字部分。
客户端:
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;
}
}
错误是:GET /?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',});