node.js - 反向代理和Socket.io,websocket握手失败

标签 node.js sockets socket.io iis-7.5 reverse-proxy

我正在 Windows Server 2008 R2 上使用 IIS 7.5 运行一个小网站。 我也有一个在端口 3000 上运行的 Node.js 应用程序。

来自网站(客户端浏览器)的 Http 调用从 http://example.com/node/whatever 反向代理到 http://localhost:3000/whatever。到目前为止一切正常。

问题是当我尝试使用 socket.io 时。

我收到:

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

如果我没记错的话,我很确定:

它确实将初始请求转发到我的服务器,因为对 websocket 服务器的初始请求是标准 HTTP 请求(带有一些附加 header )。 IIS 确实知道这一点并简单地转发请求。但是,在收到 websocket 请求后,websocket 服务器会发送 101 响应并切换到 websocket 模式。 IIS 不了解 websocket 流量,因此无法代理该流量。

是否有为 ws://地址配置反向代理的技巧或解决方案?

最佳答案

我将扩展一下我的评论。 据我了解,您有多种选择:

那么,您使用的是哪个版本的socket.io?请参阅docs对于允许的运输(摘录如下):

io.enable('browser client minification');  // send minified client
io.enable('browser client etag');          // apply etag caching logic based on version number
io.enable('browser client gzip');          // gzip the file
io.set('log level', 1);                    // reduce logging

// enable all transports (optional if you want flashsocket support, please note that some hosting
// providers do not allow you to create servers that listen on a port different than 80 or their
// default port)
io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);

关于node.js - 反向代理和Socket.io,websocket握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26983492/

相关文章:

node.js - NodeJS + Socket.io 连接断开/重新连接?

javascript - puppeteer-cluster:队列而不是执行

javascript - Node Js sequelize 原始查询更改为经纬度的 ORM

node.js - 获取错误 : invalid json response body

C# 套接字 : Client Mishandle 'a' as the Client's id

Java 代理隧道 HTTPS

node.js - 日期聚合框架

java - IOException : Broken pipe

typescript - RXJS 和 Socket.io 有什么区别

node.js - 如何将 socket.io 部署到 Google App Engine?