Ask A Question

Notifications

You’re not receiving notifications from this thread.

Action Cable with Puma, Nginx, Docker Swarm

endtoendpaper asked in Servers

I setup a DigitalOcean Ubuntu 22 droplet to host a docker swarm with containers for a rails app, postgress, and redis. I’m not able to get action cable to work with puma and nginx. I see errors in the console for: WebSocket connection to wss://mydomainname.com/cable

I followed their guides:
Setting up nginx as a reverse proxy:
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22-04

Encrypting nginx:
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

Has anyone had any luck with a similar setup?

Reply

I fixed this finally.

I updated my /etc/nginx/sites-available/mydomain.com with these additions. This is in the first server section under server_name

location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

location /cable {
proxy_pass http://127.0.0.1:3000/cable;
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;
}

I also modified my config/production.rb The HOST variable is the domain name of my server.

config.action_cable.url = "wss://#{ENV['HOST']}/cable"
config.action_cable.allowed_request_origins = ["https://#{ENV['HOST']}"]

Reply

All the tutorials I've found use puma for development and then passenger for production. But I wanted my environments to be the same!

Looking at the rails log and searching for /cable helped. The error message there seemed to indicate that nginx wasn't passing the right headers along, so I played around with the /etc/nginx/sites-available/mydomain.com file until something worked (see comment above). I'm not sure if all the settings I added are needed.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.