AWS EC2 Behind ELB Always Prints Error Unexpected response code: 400
Hey guys -
Can't seem to find out a solution for this, but when I run my app behind a load balancer, I get this error on the client:
WebSocket connection to 'wss://fakedomain.com/socket.io/?EIO=3&transport=websocket&sid=QH8VmXbiEcp3ZyiLAAAD' failed: Error during WebSocket handshake: Unexpected response code: 400
I understand the error, since it's trying to talk to the load balancer and now the EC2 instance (I'm not great with AWS so feel free to offer help on this one!) but what I don't understand is how to make the error not show up!
I'd love to fix the root cause but I'm guessing it involves a separate dedicated socket.io server to handle all the real time stuff which I don't have time for at the moment, but could someone please run me through supressing this error?
I'm assuming it's falling back to polling, which seems to work just fine (I have the socket connection connected and it fires) but I don't want to launch with a red error in my console.
Thanks in advance for any advice you might have!
Also, I've installed via bower if it matters:
"socket.io-client": "~1.1.0",
Do you have sticky connection on your server?
I assume you're not using Elastic Beanstalk (the instructions there would be much easier).
Go to EC2->Network & Security->Load Balancers
Select your load balancer and go to Listeners. Ensure that both the Load Balancer protocol and the Instance Protocol are set to TCP for port 80 and SSL for port 443 rather than HTTP and HTTPS.
Oh man. This is solid advice I haven't seen elsewhere. I'll try in the AM and report back. Thank you!
On Wed, Oct 29, 2014, 7:48 PM Vadim Kazakov [email protected] wrote:
I assume you're not using Elastic Beanstalk (the instructions there would be much easier).
Go to EC2->Network & Security->Load Balancers
Select your load balancer and go to Listeners. Ensure that both the Load Balancer protocol and the Instance Protocol are set to TCP for port 80 and SSL for port 443 rather than HTTP and HTTPS.
— Reply to this email directly or view it on GitHub https://github.com/Automattic/socket.io/issues/1846#issuecomment-61038664 .
So maybe I'm mistaken, but our site is running on HTTPS (with our SSL cert). Changing them to breaks a lot of stuff down the line. I was checking X-Forwarded-Proto in my app to make sure the request was HTTPS and if not, forcing it to redirect. Apparently there is no X-Forwarded-Proto header with SSL/TCP and Express is reporting req.secure as false (even though I typed https).
Suggestion?
Not sure, sockets use TCP/SSL protocol not HTTP so you will need to change it to TCP/SSL in order to make web sockets work. As far as how to fix the rest of your app, I have no idea.
Thanks for the help. I'll have to make a new environment and mess around a bit. There's a lot of cogs in the machine!
Is it possible to have websockets run on their own ports? If so, could you link some documentation? (I'm pretty sure there's not but I could be wrong)
They can work on whatever port you want, just specify when you connect the port like so:
io('https://myserver:123');
I'm using Elasticbeanstalk and facing the same issue, I don't have SSL installed but connected through regular http.
Hi, I have the same problem, though i'm using SockJS. my application is a Java Spring 4 application, works on development machine and getting the same error on AWS. looks like the Upgrade header is being dropped by someone, i can see it on the client.
could find a solution yet...
I am in the same boat and on a live chat with an AWS engineer and we are both stumped. I did confirm that the ELB needs to be changed from HTTP/HTTPS to TCP (or SSL (Secure TCP)) but I still get frequent 400 errors. Never had this problem until moving to Elastic Beanstalk load balanced cluster.
please try commenting any app.use middleware stuff (header/cors related) if any, or app.get("/", not sure which one worked for me, but post your results please.
Update: I have confirmed it is the ELB, whether it is configured for HTTP or HTTPS traffic, socket.io fails. When I go directly to one of the EC2s everything works great.
Attached is what I am seeing when going through the ELB.
I have seems to solved my problem by adding Route53 instance in front of the loadbalancer. i now have a working websocket with secure connections under AWS environment.
Try adding the Route 53, if this is not solving your problem i'll be happy to share my configuration.
@yoav200 think you could share your whole configuration for what made things work for you? Ie, ports on EBS security + protocol (http/tcp), any .ebextensions you needed, etc. I tried a ton of different things to hack my nginx configs to keep the upgrade headers and send them to node, but nothing worked for me. FWIW - a wiki page would be really, really helpful for this. Only thing that did work was going direct to my EC2 instance.
I have set up a Route 53 instance that manage my domain. i have configured a Hosted Zone with a record that points my domain directly to the load balancer.
the load balancer is configured to forward request to my Tomcat instances, as shown here:
the security group for ELB is pretty standard:
Are you using nginx? Did you have to do anything special configuration-wise?
I am very close to having this working, spent almost 8 hours with an AWS support engineer over the last two days. Had to rip nginx out of the mix but am very close, sockets are working fine through the ELB over http, tomorrow I think I can handle the https stuff server-side instead of at the ELB-level.
I will report back with my findings.
I don't use nginx, i manage everything at Amazon, i have moved my domain to Amazon as well, however i have migrated my application from a hosted cloud environment that uses AWS for their instances but managed the load with nginx and i had no problem there.
when asked them how it is working for them their response was:
we don't use Amazon Elastic Load Balancer, we use our own NGinx load balancing layer. A solution could be to use an Nginx or HAProxy server instead of the ELB and to use an autoscaling group with 1 node of the haproxy/nginx to ensure the high availability of the component.