manjarb4
By:
manjarb4

Rails ActionCable on Puma and Nginx not working on production after deploy via capistrano

November 1, 2017 129 views
Ruby on Rails Nginx Ubuntu Ubuntu 16.04

I create web site using ActionCable via Rails on top of Puma and Nginx on production.

But after deploy to Production server seems like ActionCable is not working because of this problem.WebSocket connection to

WebSocket connection to 
'ws://sub.domain.com/cable' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

This is my production.rb

config.action_cable.url = 'ws://sub.domain.com/cable'
config.web_socket_server_url = "ws://sub.domain.com/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
config.action_cable.disable_request_forgery_protection = true

This is my nginx.conf

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

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/deploy/apps/app_name/current/public;
  access_log /home/deploy/apps/app_name/current/log/nginx.access.log;
  error_log /home/deploy/apps/app_name/current/log/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 X-Forwarded-For $proxy_add_x_forwarded_for;
    # proxy_set_header Host $http_host;
    # proxy_set_header X-Real-IP $remote_addr;
    # proxy_set_header X-Forwarded-Proto https;
    # proxy_redirect off;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

My puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
port        ENV.fetch("PORT") { 3000 }
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
plugin :tmp_restart
environment ENV.fetch("RAILS_ENV") { "production" }
daemonize true
pidfile '/home/deploy/apps/app_name/shared/tmp/pids/puma.pid'

How can i fix this and make ActionCable work on production?

Be the first one to answer this question.