我有一个 React 应用程序与一个非常简单的 Node 后端通信,它在本地工作得很好,并且通过 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 https://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',});
关于node.js - Socket.io 无法与 nginx 和 https 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202875/