Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

My 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:

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

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

development: *local
test: *local

And my environments/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.

like image 454
Lucas Raphael Avatar asked Dec 06 '25 06:12

Lucas Raphael


1 Answers

Do you have redis installed on your machine or is it a docker container? I think you are using it with sidekiq, and do you have any sidekiq/redis initializer file under /config/initializers?

In cable.yml it should be

production:
  url: redis://redis:6379/0
like image 150
Vi. Avatar answered Dec 07 '25 20:12

Vi.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!