node.js - WebSocket 握手期间 socket.io nginx 配置错误 : 'Connection' header value is not 'Upgrade' : keep-alive

标签 node.js sockets nginx

我在服务器上的 nginx (nginx/1.1.19) 上运行 socket.io 时出错

WebSocket 握手期间出错:'Connection' header 值不是'Upgrade':keep-alive

我网站的 conf 文件是:

server{
    listen 80;
    server_name lalala.com;
    access_log /home/hao/sites/reactjsweekly/accesss.log;
    error_log /home/hao/sites/reactjsweekly/error.log;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:3002/;
        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

后端的socket.io:

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var io = require('socket.io').listen(server);  

io.sockets.on('connection', function (socket) {
socket.emit('info', {data: "lala"});



  });

});

以前有人遇到过同样的问题吗???

最佳答案

[1] “从 1.3.13 版本开始,nginx 实现了特殊的操作模式,如果代理服务器返回代码为 101(切换协议(protocol))的响应,并且客户端通过以下方式请求协议(protocol)切换,则允许在客户端和代理服务器之间建立隧道请求中的“升级” header 。”

您的版本是1.1.19;升级,它应该按预期工作。

关于node.js - WebSocket 握手期间 socket.io nginx 配置错误 : 'Connection' header value is not 'Upgrade' : keep-alive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506050/

相关文章:

node.js - Websocket 与 Socket.io 和 Angular

node.js - 获取本地和全局安装的nodejs模块

c# - C#异步Socket.BeginSendFile仅发送两个异步文件

django - 在 systemd 上使用 daphne 运行 django channel

javascript - 缓冲区使用说明

尝试通过 SSL 与后端服务器通信的 Node.js 错误

c++ - 操作 Windows 资源管理器窗口时 UDP 数据包丢失

c# - 在 C# 中解析原始 HTTP 响应以获取状态代码

nginx - nginx proxy_pass 中的变量导致 MIME 类型问题

docker - 需要 'y'响应的Docker构建镜像