title: Rails + ActionCable + Nginx + Unicorn Setup
body: <p>I've been trying to deploy a Rails 5 + ActionCable app in Ubuntu using Nginx + Unicorn.</p>
  
  <p>Normal http request work just fine, but Websockets connection are rejected, I suppose is an problem with my Nginx.</p>
  
  <p>I been wondering in Google, found similar questions and solutions but nothing seem to work:</p>
  
  <p>-<a href="https://stackoverflow.com/questions/36259421/how-to-configure-actioncable-with-nginx-and-unicorn-in-production">How to configure ActionCable with Nginx and Unicorn in production?</a></p>
  
  <p>-<a href="https://stackoverflow.com/questions/34680836/nginx-configuration-for-rails-5-actioncable-with-puma">NGINX configuration for Rails 5 ActionCable with puma</a></p>
  
  <p>-<a href="https://stackoverflow.com/questions/35928227/rails-5-action-cable-deployment-with-nginx-puma-redis">Rails 5 Action Cable deployment with Nginx, Puma &amp; Redis</a></p>
  
  <p>I even try adapting NodeJS setup for nginx and websockets
  <a href="https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/" rel="nofollow noreferrer">https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/</a></p>
  
  <p>The error I get is 'ws://mydomain.com/cable' failed: Error during WebSocket handshake: 404</p>
  
  <pre><code>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;
  }
  
  }
  </code></pre>
  
  <p>Thanks in advance</p>