2
votes

I've been trying to deploy a Rails 5 + ActionCable app in Ubuntu using Nginx + Unicorn.

Normal http request work just fine, but Websockets connection are rejected, I suppose is an problem with my Nginx.

I been wondering in Google, found similar questions and solutions but nothing seem to work:

-How to configure ActionCable with Nginx and Unicorn in production?

-NGINX configuration for Rails 5 ActionCable with puma

-Rails 5 Action Cable deployment with Nginx, Puma & Redis

I even try adapting NodeJS setup for nginx and websockets https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/

The error I get is 'ws://mydomain.com/cable' failed: Error during WebSocket handshake: 404

upstream app_server {
   server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
   listen   80;
   root /home/rails/rails_project/public;
   server_name _;
   index index.htm index.html;

   location / {
        try_files $uri/index.html $uri.html $uri @app;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|ta$
                try_files $uri @app;
        }

 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_server;
}
location /cable {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://app_server;
}

}

Thanks in advance

1

1 Answers

0
votes

Fixed similar error (404 in actioncable /cable) in my setup like this

location /cable {
    proxy_pass http://unix:/home/app/shared/tmp/puma/socket;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}