node.js – socket.io nginx配置WebSocket握手期间出错:’Connection’标头值不是’Upgrade’:keep-alive

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

WebSocket握手期间出错:’Connection’标头值不是’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(交换协议)的响应,则允许在客户端和代理服务器之间建立隧道,并且客户端要求协议交换机通过请求中的“升级”标题.“

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

转载注明原文:node.js – socket.io nginx配置WebSocket握手期间出错:’Connection’标头值不是’Upgrade’:keep-alive - 代码日志