socket.io nginx配置WebSocket握手期间出错:“连接"标头值不为“升级":keep-alive [英] socket.io nginx configuration Error during WebSocket handshake: 'Connection' header value is not 'Upgrade': keep-alive

查看:472
本文介绍了socket.io nginx配置WebSocket握手期间出错:“连接"标头值不为“升级":keep-alive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got an error when running socket.io on nginx (nginx/1.1.19) on my server

在WebSocket握手期间出错:连接"标头值不是升级":保持活动状态

Error during WebSocket handshake: 'Connection' header value is not 'Upgrade': keep-alive

我的网站conf文件为:

My conf file for my website is:

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:

socket.io on the backend side:

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"});



  });

});

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

anyone ran into the same issue before???

推荐答案

[1] 从1.3.13版开始,nginx实施了一种特殊的操作模式,如果代理服务器返回了代码为101(交换协议)的响应,并且客户端要求提供一个响应,则该模式允许在客户端和代理服务器之间建立隧道.通过请求中的升级"标头进行协议切换."

[1] "Since version 1.3.13, nginx implements special mode of operation that allows setting up a tunnel between a client and proxied server if the proxied server returned a response with the code 101 (Switching Protocols), and the client asked for a protocol switch via the "Upgrade" header in a request."

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

Your version is 1.1.19; upgrade and it should work as expected.

这篇关于socket.io nginx配置WebSocket握手期间出错:“连接"标头值不为“升级":keep-alive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆