Answer a question

There is a staging environment which is setup using Ubunut, Nginx, Puma and Ruby on Rails application. It is working fine. I'm trying to create same separate environment and can't setup WebSockets. I need some help with figuring out what is wrong, why WebSocket connection fails. Currently it is showing following error:

WebSocket connection to 'wss://upgrade.mysite.com/cable' failed: application-2c9281fcfd42a4b226b2bec3c0a6f9aaca5f7295cefd1099d252d3689e9e19d0.js:49276 

The Nginx server is configured for basic authentication and SSL

Following is the WebSocket configuration in the sites-available/mysite:

upstream app {
    server unix:/home/myuser/mysite/current/tmp/sockets/puma.sock fail_timeout=0;
}

server {

... 

location @app {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://app;
    }

    location /cable {
                proxy_pass http://app;
                proxy_http_version 1.1;
                proxy_set_header Upgrade websocket;
                proxy_set_header Connection Upgrade;
                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;
    }

    ...

}

I'm not sure whether any other setup is need or how to debug WebSocket connection. Any help is appreciated. Even suggestion how to debug it would matter for me.

The ruby on rails site works fine, only WebSocket fails to connect. Deployments are done using Capistrano.

Answers

The problem was in the config/application.rb file. I've set correctly the following config option: config.action_cable.url = https://test.mysite.com however didn't set the config.action_cable.allowed_request_origins

Correct config/application.rb file should look like this:

require 'active_model/railtie'
require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'

Bundler.require(*Rails.groups)

module MyModule
  class Application < Rails::Application

  ...

  config.action_cable.url = https://test.mysite.com
  config.action_cable.allowed_request_origins = [/https:\/\/test.mysite.com/]
  
  ...

  end
end
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐