【问题标题】:Rails 6 ActionCable Unable to Upgrade WebSocket RequestRails 6 ActionCable 无法升级 WebSocket 请求
【发布时间】:2022-02-26 10:09:50
【问题描述】:

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

问题描述

我正在使用以下设置:

  • Ruby 2.7.5
  • Rails 6.1.0
  • GraphQL
  • React 前端(单独的仓库)
  • 弹力豆茎
    • Ruby 2.7 在 64 位 Amazon Linux 2/3.4.1 上运行
  • 应用负载均衡器
  • Postgres ActionCable 适配器

我的应用程序已部署到 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"

【问题讨论】:

    标签: ruby-on-rails nginx websocket amazon-elastic-beanstalk actioncable


    【解决方案1】:

    在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-09
      • 2017-01-19
      • 2018-06-03
      • 2019-05-03
      • 2016-06-15
      • 1970-01-01
      • 2014-06-24
      • 2018-09-22
      相关资源
      最近更新 更多