Ruby on rails Rails 6在WebSocket握手期间发生生产ActionCable错误

Ruby on rails Rails 6在WebSocket握手期间发生生产ActionCable错误,ruby-on-rails,amazon-ec2,websocket,ruby-on-rails-6,actioncable,Ruby On Rails,Amazon Ec2,Websocket,Ruby On Rails 6,Actioncable,伙计们,我在这里经常看到这个(老)问题,所以它看起来可能是重复的。但在我的情况下,我非常努力地修正这个错误,但我做不到。 我也遵循这个修正:但没有成功 My nginx.conf: WebSocket connection to 'ws://my-ec2/cable' failed: Error during WebSocket handshake: Unexpected response code: 404 在我的cable.yml中: upstream puma { server

伙计们,我在这里经常看到这个(老)问题,所以它看起来可能是重复的。但在我的情况下,我非常努力地修正这个错误,但我做不到。 我也遵循这个修正:但没有成功

My nginx.conf:

WebSocket connection to 'ws://my-ec2/cable' failed: 
Error during WebSocket handshake: Unexpected response code: 404
在我的cable.yml中:

upstream puma {
    server unix:///home/ubuntu/apps/my_app/shared/tmp/sockets/my_app-puma.sock;
}

server {
    listen 80 default_server deferred;

    # If you're planning on using SSL (which you should), you can also go ahead and fill out the following server_name variable:
    # server_name example.com;

    # Don't forget to update these, too
    root /home/ubuntu/apps/my_app/current/public;
    access_log /var/log/nginx/nginx.access.log;
    error_log /var/log/nginx/nginx.error.log info;

    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    try_files $uri/index.html $uri @puma;
    location @puma {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass http://puma;
    }

    location /cable {
        proxy_pass http://puma;
        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-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_request_headers on;

        proxy_buffering off;
        proxy_redirect off;
        break;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 10M;
    keepalive_timeout 10;
}
以及我的环境/production.rb:

production:
  url: redis://http://my-ec2.com:6379

local: &local
  url: redis://localhost:6379

development: *local
test: *local
为那些有这种问题的人想想这一天:

在本地主机上设置ActionCable已经是一场很好的战斗,但在生产环境中设置是一场完整的战争


您的机器上安装了redis还是docker容器?我认为您正在将其与sidekiq一起使用,并且在
/config/initializers
下是否有任何sidekiq/redis初始值设定项文件

cable.yml

Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true

  config.consider_all_requests_local       = false


  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?



  config.active_storage.service = :local

  config.action_cable.mount_path = '/cable'
  config.action_cable.url = 'ws://my-ec2/cable'
  config.action_cable.allow_same_origin_as_host = true
  config.action_cable.allowed_request_origins = ["*"]


  config.log_level = :debug

  config.log_tags = [ :request_id ]



  config.action_mailer.perform_caching = false


  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new


  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  config.active_record.dump_schema_after_migration = false

end


您的机器上安装了redis还是docker容器?我认为您正在将其与sidekiq一起使用,并且在
/config/initializers
下是否有任何sidekiq/redis初始值设定项文件

cable.yml

Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true

  config.consider_all_requests_local       = false


  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?



  config.active_storage.service = :local

  config.action_cable.mount_path = '/cable'
  config.action_cable.url = 'ws://my-ec2/cable'
  config.action_cable.allow_same_origin_as_host = true
  config.action_cable.allowed_request_origins = ["*"]


  config.log_level = :debug

  config.log_tags = [ :request_id ]



  config.action_mailer.perform_caching = false


  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new


  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  config.active_record.dump_schema_after_migration = false

end


几天后,我自己设法解决了这个问题。 我出错的主要原因是,在environments/production.rb文件中,我说actioncable端点是ec2的公共ip。但实际上,您应该放置localhost。我使用了development.rb中相同的配置:

production.rb之前:

production:
  url: redis://redis:6379/0
production.rb之后:

...
   config.action_cable.mount_path = '/cable'
   config.action_cable.url = 'ws://my_ec2/cable'
   config.action_cable.allow_same_origin_as_host = true
   config.action_cable.allowed_request_origins = ["*"]
...

几天后,我自己设法解决了这个问题。 我出错的主要原因是,在environments/production.rb文件中,我说actioncable端点是ec2的公共ip。但实际上,您应该放置localhost。我使用了development.rb中相同的配置:

production.rb之前:

production:
  url: redis://redis:6379/0
production.rb之后:

...
   config.action_cable.mount_path = '/cable'
   config.action_cable.url = 'ws://my_ec2/cable'
   config.action_cable.allow_same_origin_as_host = true
   config.action_cable.allowed_request_origins = ["*"]
...

我解决了那个问题。我将补充一个答案。谢谢我解决了那个问题。我将补充一个答案。谢谢