Question

Rails 7 Action Cable with Puma, Nginx, Docker Swarm

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 browser console for:

WebSocket connection to wss://mydomain.com/cable failed

I followed these 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

My /etc/nginx/sites-available/mydomain.com

server {
    server_name mydomain.com www.mydomain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        include proxy_params;
    }
    
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name mydomain.com www.mydomain.com;
    return 404; # managed by Certbot

}

My cable.yml

development:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://redis:6379" } %>
  channel_prefix: study_notes_development

test:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://redis:6379" } %>
  channel_prefix: study_notes_test

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://redis:6379" } %>
  channel_prefix: study_notes

my docker-stack.yml

version: '3'

services:
  db:
    image: postgres
    volumes:
      - db_data:/var/lib/postgresql/data
    env_file:
      - .env.production.local
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
  redis:
    image: redis:latest
    volumes:
      - redis:/data
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
  cron:
    image: myimage:cron
    env_file:
      - .env.production
      - .env.production.local
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
  web:
    image: myimage:prod
    env_file:
      - .env.production
      - .env.production.local
    ports:
      - "3000:3000"
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
  db_migrator:
    image: myimage:prod
    command: ["./wait-for","--timeout=300","db:5432","--","bin/rails","db:migrate","db:seed"]
    env_file:
      - .env.production
      - .env.production.local
    deploy:
      restart_policy:
        condition: none

volumes:
  db_data:
  redis:
Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more