基于 SSL 的 Websocket (WSS)“升级失败”Elastic Beanstalk(ALB、Rails、Nginx、Actioncable)

way*_*nep 5 ruby-on-rails nginx amazon-web-services websocket amazon-elastic-beanstalk

当尝试连接到部署在 ALB 后面的 EB 上的 Websocket 时,我收到 404 升级失败的消息。大多数与此相关的问题都没有得到解答或不清楚。

真实域名替换为 example.com

> new WebSocket('wss://example.com/cable');
WebSocket connection to 'wss://example.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
Run Code Online (Sandbox Code Playgroud)

/var/app/current/log/Production.log 中出现错误(行已缩短)

I, Started GET "/cable" 
I, Started GET "/cable/"[non-WebSocket] 
E, Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
I, Finished "/cable/"[non-WebSocket] 
Run Code Online (Sandbox Code Playgroud)

设置

  • Elastic Beanstalk + ALB(Puma 与 Ruby 2.4)
  • HTTPS/WSS/SSL
  • 轨道5

配置/生产.rb:

config.action_cable.url = "wss://usetreasure.com/cable"
config.action_cable.allowed_request_origins = [ '/https:\/\/*/', '/http:\/\/*/' ]
Run Code Online (Sandbox Code Playgroud)

在 EB 实例上 /etc/nginx/conf.d/websockets.conf

upstream puma {
  server unix://var/run/puma/my_app.sock;
}

server {
  listen 443;
  server_name example.com;

  location /cable {
    proxy_http_version 1.1;
    proxy_pass http://puma;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
Run Code Online (Sandbox Code Playgroud)

ALB 在 HTTP / HTTPS 上设置为 443 和 80(但在安全组上显示 TCP?) 图像在这里