简体   繁体   English

在 WebSocket 握手期间 Rails 6 Production ActionCable 错误

[英]Rails 6 Production ActionCable Error during WebSocket handshake

WebSocket connection to 'ws://my-ec2/cable' failed: 
Error during WebSocket handshake: Unexpected response code: 404

Guys, I saw this (old) issue a lot here, so it may seem like it is duplicated.伙计们,我在这里经常看到这个(旧)问题,所以它看起来像是重复的。 But in my case, I try very hard to fix this error but I can't.但就我而言,我非常努力地修复这个错误,但我不能。 I also follow this correction: https://stackoverflow.com/a/55715218/8478892 but no success.我也遵循此更正: https://stackoverflow.com/a/55715218/8478892但没有成功。

My nginx.conf:我的 nginx.conf:

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

And in my cable.yml:在我的 cable.yml 中:

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

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

development: *local
test: *local

And my environments/production.rb:还有我的环境/production.rb:

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

Thought of the day for those who are having this problem:为遇到此问题的人考虑的一天:

Setting up ActionCable en localhost had already been a good fight, but setting up in production is an entire war.在 localhost 上设置 ActionCable 已经是一场精彩的战斗,但在生产环境中设置则是一场完整的战争。

Do you have redis installed on your machine or is it a docker container?您的机器上安装了 redis 还是 docker 容器? I think you are using it with sidekiq, and do you have any sidekiq/redis initializer file under /config/initializers ?我认为您正在将它与 sidekiq 一起使用,并且/config/initializers下是否有任何 sidekiq/redis 初始化程序文件?

In cable.yml it should becable.yml中应该是

production:
  url: redis://redis:6379/0

After a few days, I managed to solve this problem myself.几天后,我设法自己解决了这个问题。 The main reason for my error is that in my environments / production.rb file, I was saying that the actioncable endpoint was the public ip of my ec2.我的错误的主要原因是在我的环境/production.rb 文件中,我说可操作端点是我的 ec2 的公共 ip。 But in reality you should put localhost.但实际上你应该把本地主机。 I used the same configuration I have in development.rb:我使用了与 development.rb 中相同的配置:

production.rb before: 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 = ["*"]
...

production.rb after: production.rb 之后:

...
  config.action_cable.disable_request_forgery_protection = true
   config.action_cable.url = "ws://localhost:3000/cable"
   config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
   config.action_cable.allowed_request_origins = /(\.dev$)|^localhost$/
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在websocket握手期间Rails ActionCable错误 - Rails ActionCable error during websocket handshake Rails 5 Actioncable-失败:WebSocket握手期间出错:意外的响应代码:502 - Rails 5 Actioncable - failed: Error during WebSocket handshake: Unexpected response code: 502 ActionCable Rails 5(Passenger) - 失败:WebSocket握手期间出错:意外响应代码:404 - ActionCable Rails 5 (Passenger) - failed: Error during WebSocket handshake: Unexpected response code: 404 ActionCable - WebSocket 握手期间出错:意外响应代码:404 - ActionCable - Error during WebSocket handshake: Unexpected response code: 404 WebSocket 握手期间的 Rails 5 错误:意外响应代码:502 - Rails 5 Error during WebSocket handshake: Unexpected response code: 502 Websocket Rails 在握手期间获得 404 - Websocket Rails getting 404 during handshake websocket-rails,websocket握手错误 - websocket-rails, websocket handshake error ActionCable - 无法在生产中升级到 WebSocket - ActionCable - Failed to upgrade to WebSocket in production Actioncable Nginx和Puma WebSocket握手:意外响应 - Actioncable Nginx and Puma WebSocket handshake: Unexpected response 带Rails(Puma)的Websockets-WebSocket握手期间出错:意外的响应代码:200 - Websockets with Rails(Puma) - Error during WebSocket handshake: Unexpected response code: 200
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM