30

I'm trying to setup a websocket-server with socket.io on AWS Elastic Beanstalk. Got the initial request to /socket.io/1/?t=xxxx up and running but the websocket handshake fails with this error:

WebSocket connection to 'ws://xxxxxx.elasticbeanstalk.com/socket.io/1/websocket/a4DTCN2BeJZuiSaphVyz' failed: Unexpected response code: 502 

From googling and looking at the error 502 (Bad Gateway) it seems to have something to do with the nginx-proxy. So how do I configure the nginx to work with websockets?

Can't find anything in AWS documentation about setting up for websockets.

funparko
  • 303
  • 1
  • 3
  • 4

3 Answers3

28

you could try to go through the trouble of configuring nginx, but if you are only using websockets, it would be easier to turn off all proxys and directly connect.

this is under your app environment in the elastic beanstalk dashboard. There's a drop down button on the right marked "Actions". Select "Edit/Load Configuration"

enter image description here

Adam
  • 901
  • 2
  • 10
  • 13
  • 5
    As of 2014-04-13 this is located under: //Configuration/Software Configuration/Container Options. – Rob Apr 13 '14 at 18:48
  • Does "easier" also mean better? Like, what do you get by setting the proxy server to nginx (besides static file mappings and gzip compression, both of which I don't need). What about performance & security? I'd suspect performance is better with no proxy server. But, what about security? Is nginx better for WSS (WebSockets over SSL/TLS), or should I just implement [SSL with Node.js](https://github.com/einaros/ws/blob/master/examples/ssl.js)? What about DoS protection? Anything else? – ma11hew28 Nov 08 '14 at 19:17
  • 1
    I've proxy server off, but I'm getting an error connection is closed before it's established, question posted here: http://stackoverflow.com/questions/26880396/socket-io-error-web-socket-connection-is-closed-before-the-connection-is-estab – user2727195 Nov 12 '14 at 06:42
  • Where can you turn off nginx? It's no longer under Container Options. – Daniel van Flymen Feb 22 '16 at 02:37
  • 8
    I think the accepted answer should include an solution without turning off nginx. – sdgfsdh Aug 17 '16 at 23:39
  • 1
    To get this to work I also had to update the load balancer to use tcp. https://github.com/socketio/socket.io/issues/1846#issuecomment-61038664 – gkiely Sep 18 '16 at 21:37
  • This doesn´t work anymore, as of 2017-02-03 Currently only the Nodejs container allows you to turn-off the proxy on the instance. – Oscar Nevarez Mar 02 '17 at 20:02
4

There is a significant disadvantage with the ditch-nginx approach. You will have to move https into node and also to your socket.io calls. It's much easier to manage https in the server configuration rather than in node.

If you don't need https I suggest dumping the proxy and just going directly to the node.

Michael Draper
  • 1,928
  • 3
  • 18
  • 24
4

Socket.io defaults to port 8080. Make sure port 8080 is enabled on your EC2 Security Group by adding a "Inbound > Custom TCP Rule" in the AWS Admin > EC2 > Security Groups.

jp_inc
  • 345
  • 4
  • 14