Websocket 在 AWS 应用程序负载均衡器或网络负载均衡器上启用



我花了几个小时试图想出一个解决方案,并阅读了很多带有nginx的网络套接字解决方案,但仍然没有运气

我有一个使用 docker 容器化的 websocket 应用程序,并使用 ecs 在 ec2 实例上运行。 我的 websocket 需要能够在需要时自动扩展。

我已经测试了与经典 elb 的连接,一切正常,但它不支持 websocket 协议。

下至ALB和NLB

ALB 只允许 HTTP 和 HTTPS 协议并支持 websockets,我不确定如何实现它以实现通过 WSS 协议访问我的 websocket,目标组健康检查失败。

NLB 运行良好,因为它允许 TCP 协议,但唯一的问题是它不会终止 SSL。

唯一的解决方案是在EC2上安装SSL证书,并将nginx反向代理设置为docker容器。 但我对此没有任何乐趣,因为我没有使用 Nginx 的经验,我可能没有正确的配置。 但我仍然无法通过WSS连接到WebSocket。欢迎任何帮助

我的主要目标是通过 wss 连接到 websocket。

worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-nginx {
server nginx:443;
ssl_certificate /etc/ssl/private/example.chained.crt;
ssl_certificate_key /etc/ssl/private/example.key;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
}
upstream localhost {
server apache:443;
}
server {
listen 443;
location / {
proxy_pass         http://localhost;
proxy_redirect     off;
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-Host $server_name;
}
}
server {
listen 443;
location / {
proxy_pass         http://example.com;
proxy_redirect     off;
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-Host $server_name;
}
}
}

这是通过使用 ALB 解决的。

最新更新