Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 789
Serhii Matrunchyk Avatar asked Dec 19 '22 04:12

Serhii Matrunchyk


1 Answers

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.

like image 138
Steve E. Avatar answered Apr 26 '23 06:04

Steve E.