Rails 6 ActionCable 无法升级 WebSocket 请求

Rails 6 ActionCable Unable to Upgrade WebSocket Request

一段时间以来,我一直在努力让我的 Rails 应用程序正确部署,现在终于决定是时候向社区寻求帮助了。我已经阅读了关于这个问题的几乎所有 Whosebug post,包括以下内容,但没有运气:

问题描述

我正在使用以下设置:

我的应用程序已部署到 AWS Elasticbeanstalk,并且对 /graphql 的所有请求都成功了。但是,当尝试连接到 /cable 时,我在浏览器控制台中收到此错误:

WebSocket connection to 'wss://api.redacted.io/cable' failed: 

在检查 Elastic Beanstalk 日志时,我看到: /var/app/containerfiles/logs/production.log:

I, [2022-02-20T19:35:25.849990 #32761]  INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Started GET "/cable" for <redacted IP> at 2022-02-20 19:35:25 +0000
I, [2022-02-20T19:35:25.850342 #32761]  INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Started GET "/cable/"[non-WebSocket] for <redacted IP> at 2022-02-20 19:35:25 +0000
E, [2022-02-20T19:35:25.850384 #32761] ERROR -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
I, [2022-02-20T19:35:25.850419 #32761]  INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Finished "/cable/"[non-WebSocket] for <redacted IP> at 2022-02-20 19:35:25 +0000

可能相关的文件

cable.yml:

development:
  adapter: postgresql

test:
  adapter: test

production:
  adapter: postgresql

production.rb:

...
config.action_cable.url = 'wss://api.redacted.io/cable'

config.action_cable.allowed_request_origins = ['redacted.io', 'http://redacted.io', 'https://redacted.io']
...

.ebextensions/nginx_proxy.config:

files:
  "/etc/nginx/conf.d/websockets.conf" :
    content: |
      upstream backend {
          server unix:///var/run/puma/my_app.sock;
      }

      server_names_hash_bucket_size 128;

      server {
          listen 80;

          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log;

          server_name redacted.elasticbeanstalk.com;

          # prevents 502 bad gateway error
          large_client_header_buffers 8 32k;

          location / {
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header Host $http_host;
              proxy_set_header X-NginX-Proxy true;

              # prevents 502 bad gateway error
              proxy_buffers 8 32k;
              proxy_buffer_size 64k;

              proxy_pass http://backend;
              proxy_redirect off;

              location /assets {
                root /var/app/current/public;
              }

              # enables WS support
              location /cable {
                proxy_pass http://backend/cable;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
              }
          }
      }


container_commands:
  01restart_nginx:
    command: "nginx -t && service nginx restart"

在 reddit 上发帖后,我可以通过以下方式解决我的问题:

  1. 正在删除我的 .ebextensions/nginx_proxy.config 文件。
  2. 正在创建一个新文件,.platform/nginx/conf.d/elasticbeanstalk/websocket.conf 内容为:
location /cable {
  proxy_pass http://my_app/cable;
  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;
}