Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails5 Action Cable Nginx 404 and 502 errors

Everyone :). I know people have already faced alot of problems related to mine. I have tried all but my issue has not been resolved. I have been working from past 3 days to fix this but I am unable to do it.

I am using ActionCable for the first time and on development server it is working fine. But in production where I am using Puma and Nginx I am facing terrible issues.

Initially when I had not (location /cable) settings in nginx configuration, server gives me 404 handshake error
i.e Error during WebSocket handshake: Unexpected response code: 404

Then after I add following location /cable configuration in nginx configuration I start getting 502 bad gateway error.

Note: I have not opened any port specifically for ActionCable. I assume it is not required. only port 80 is open on my server.

I need some expert to help me with this. I need quick help to get it fixed. Thanks in advance :)

I have these two lines present in my environment/production.rb

config.action_cable.url = "ws://my_linode_domain/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]

This is my nginx config file

    upstream app {
      # Path to Puma SOCK file, as defined previously
      server unix:/home/deploy/artcrate/shared/tmp/sockets/puma.sock fail_timeout=0;
    }

    server {
      listen 80;
      #server_name localhost;
      server_name my_linode_domain

     # prevents 502 bad gateway error
     large_client_header_buffers 8 32k;

      root /home/deploy/artcrate/current/public;

      try_files $uri/index.html $uri @app;

      location / {
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Connection '';
        proxy_pass http://app;
      }
      location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    location /cable{
       proxy_pass http://app;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
     }

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

I have tried various proxy_pass options in location /cable settings but none worked.

like image 437
omair azam Avatar asked Dec 02 '25 14:12

omair azam


1 Answers

Everyone. :)

After a week of struggle, hardwork and constantly playing around with nginx and puma configuration files and keenly reading blogs again and again, I was able to figure out the issue.

My nginx configurations were correct. I had to add two more lines to puma.rb configuration which don't come with default configurations. Those two lines are:

workers 2
daemonize true

daemonize true: this tells puma to run in the background by spawning a subprocess and detaching it from the executing shell. If you don't use daemonize, you need to run the puma process via nohup and put it in the background explicitly.

I am not sure if I required workers 2 but I had added them while resolving my issue. So I let it there. But after adding above two lines my ActionCable started to work normally.

like image 87
omair azam Avatar answered Dec 04 '25 07:12

omair azam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!