AWS EB: Error during WebSocket handshake: Unexpected response code: 400

8.8k views Asked by At

There is a Laravel/Vue.JS app hosted on AWS behind a Classic Load Balancer (Elastic Beanstalk) and proxied internally via Nginx down to socket.io server. SSL is terminated on the Nginx.

This is the nginx config:

location /socket.io {
    proxy_pass          http://127.0.0.1:6001;
    proxy_http_version  1.1;
    proxy_set_header    Upgrade           $http_upgrade;
    proxy_set_header    Connection        "upgrade";
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP         $remote_addr;
    proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto $scheme;
}

Right now the long-polling mode works fine, but it fails to initiate an upgrade:

WebSocket connection to 'wss://example.com/socket.io/?EIO=3&transport=websocket&sid=HmDFtq-aj1WgfGUyAAAJ' failed: Error during WebSocket handshake: Unexpected response code: 400

P.S Chrome's Frames tab I can only see this weird message: (Opcode -1)

Has anybody successfully got socket.io working on an AWS Elastic Beanstalk environment? I just wasted two weeks dealing with this issue, would be very thankful for ANY suggestions or ideas. Thanks!

Update. I turned on a verbose logging and here are the variables within Nginx:

$host example.com
$proxy_add_x_forwarded_for 134.xxx.xxx.xxx
$http_upgrade -
$remote_addr 172.31.10.208
$remote_user -
$server_name _
$upstream_addr 127.0.0.1:6001
$request GET /socket.io/?EIO=3&transport=polling&t=Lw26sYn&sid=6L5iHma-GJOeE3JQAAAX HTTP/1.1
$upstream_response_time 24.658 msec
$request_time 24.658

Maybe someone will find some of these values incorrect so I would appreciate any advise.

4

There are 4 answers

6
Steve E. On BEST ANSWER

Is your ELB using HTTP/HTTP listeners or TCP/SSL listeners? Websockets only works on the latter protocol types. Change the listener to TCP and it will work.

Alternatively, if you built your environment using CLI or API, you can also rebuild your ElasticBeanstalk App using an Application Load Balancer (ALB) instead of a Classic Load Balancer (ELB) as the ALB also supports websockets. This option is not available via the web console.

0
levacjeep On

Try switching from the Apache proxy server to Nginx in your ElasicBeanStalk "Software" configuration.

0
Kristian On

Another option is to just use the New Application balancer which does not come by default when running elasticbeanstalk

Could create a new environment with the new type or try migrating the classic balancer to Application(I made it with clean recreate)

Once create add one more Listener for port 443 HTTPS and everything worked out of the box.

Chris

0
Aaqib Sheikh On

I was facing same issue and backend was on aws elasticbeanstalk, so we set Load Balancer, to handle multiple request calls and this error was fixed. So I think you need to configure Load Balancer in amazon elastic beanstalk. Also change your ec2 instance RAM 2GB+, or t2.large