8

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

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

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 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???

Hao
  • 1,476
  • 3
  • 15
  • 20
  • 2
    You need to update your Nginx to 1.3 or higher, as this answer suggests. http://stackoverflow.com/questions/20718245/websocket-connection-failed-with-nginx-nodejs-and-socket-io If that doesn't work then try `"Upgrade"` instead of `"upgrade"`. – Ben Fortune Mar 19 '14 at 13:17
  • Thanks, upgrading to 1.5 works!! – Hao Mar 19 '14 at 15:01

2 Answers2

1

[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."

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

Kris Molinari
  • 503
  • 6
  • 17
0

Several implementations check for Upgrade << capitalized.

proxy_set_header Connection "upgrade";

should be Capitalized

proxy_set_header Connection "Upgrade";

Ryan Kopf
  • 404
  • 4
  • 9