Rails 7 Action Cable 与 Puma、Nginx、Docker Swarm

问题描述 投票:0回答:1

我设置了一个 DigitalOcean Ubuntu 22 Droplet 来托管一个 docker 群,其中包含用于 Rails 应用程序、Postgress 和 Redis 的容器。我无法让 Action Cable 与 puma 和 nginx 一起使用。我在浏览器控制台中看到以下错误:

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

我遵循了这些指南:将 nginx 设置为反向代理:https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22 -04

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

我的/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

}

我的电缆.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

我的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:

我尝试了在其他问题中找到的 nginx 设置的变体,例如这个问题的公认答案:我需要做什么才能在 nginx 和 puma 上连接 ActionCable?

nginx docker-swarm puma ruby-on-rails-7 ubuntu-22.04
1个回答
0
投票

我更新了我的 /etc/nginx/sites-available/mydomain.com 这是在 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;
}

我还修改了我的config/development.rb HOST变量是我服务器的域名。

  config.action_cable.url = "wss://#{ENV['HOST']}/cable"
  config.action_cable.allowed_request_origins = ["https://#{ENV['HOST']}"]
© www.soinside.com 2019 - 2024. All rights reserved.