'Socket.io + node js behind AWS ALB not working properly
I needed a proxy service where I can pass as a header the ip address of the upstream. What I have is:
- node js + socket.io server that works locally
- Nginx behind an AWS ALB(the ALB is working on HTTPS and the Nginx works on HTTP so that the ALB is used as an SSL terminator.
I have the following configuration for Nginx:
server {
listen 80;
error_log stderr;
access_log /dev/stdout;
underscores_in_headers on;
location / {
proxy_pass_request_headers on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 86400;
if ($http_X_Pass_To = "") {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers 'X-Pass-To,User-Agent,Content-Type';
return 200;
}
proxy_pass http://$http_X_Pass_To$request_uri;
}
location /health {
return 200 "healthy\n";
}
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
It reads the X-Pass-To header and forwards the request to the specified target.
The problem is that when I run the client it is working but I get this strange error which is not shown on local development:
WebSocket connection to 'wss://<my-domain>/socket.io/?EIO=4&transport=websocket&sid=XggNamuD4_kpe2-JAABE' failed:
Also, when I use the client I get a lot of xhr requests instead of one as it works locally. And in the network tab I have one finished ws connection that shows N/A messages.
I have absolutely no idea what the problem may be. I would be quite happy if some of you know better what may be the reason for this strange behaviour.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
