nginx作为NodeJS + socket.io的代理:客户端没有收到通知

Hil*_*ilo 3 nginx websocket node.js socket.io

我有nginx配置代理如下的websockets:

location /socket.io {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
Run Code Online (Sandbox Code Playgroud)

}

客户端连接正常,但是当服务器发出通知时:

io.sockets.emit("update",data);
Run Code Online (Sandbox Code Playgroud)

客户永远不会收到它:

var socket = io.connect("http://" + hostip + "/socket.io");
socket.on("update",function(data)
{
  console.log("got update: " + data);
});
Run Code Online (Sandbox Code Playgroud)

如果我绕过nginx,一切都按预期工作:

var socket = io.connect("http://" + hostip + ":8000");
socket.on("update",function(data)
{
  console.log("got update: " + data);
});
Run Code Online (Sandbox Code Playgroud)

我错过了什么?顺便说一句,为什么不绕过nginx单一来源策略违规?谢谢.

Pet*_*ons 5

尝试var socket = io.connect(location.host);在浏览器中.我不认为你想要这/socket.io条路.