2

I am trying to configure the server with rails 5, Nginx and Puma. The application is running fine but Actioncable is giving

WebSocket connection to 'ws://server_name.com/cable' failed:
Error during WebSocket handshake: Unexpected response code: 200

Below are my nginx settings,

upstream app {
  server unix:/tmp/app.sock fail_timeout=0;
}

server {
    listen       80;
    server_name  server_name.com;
    try_files $uri/index.html $uri @app;
    client_max_body_size 100M;
    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app;
      client_max_body_size 10M;
    }

    location /cable {
       proxy_pass http://app/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
    }

    location ~ ^/(assets|uploads)/ {
        root assets_path;
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }
    error_page   500 502 503 504  /500.html;
 }

In rails in production.rb, I did the settings like below.

config.action_cable.url = 'ws://server_name.com/cable'

Any help will be appreciated.

Muaaz Rafi
  • 1,469
  • 2
  • 15
  • 23

2 Answers2

2

try to add:

config.action_cable.allowed_request_origins = ['*']
config.action_cable.disable_request_forgery_protection = true

to your config/environments/production.rb file

it works with me, you can check this link also. and this question

Abdallah Okasha
  • 1,879
  • 17
  • 20
1

Try using,

location /cable {
    proxy_pass http://app; # not http://app/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

And also make sure that you have config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/] in your production.rb if you don't have ssl

Abhishek
  • 331
  • 3
  • 7