在生产环境中使用Action Cable + Redis(ElastiCache)

每次部署时都感到困惑,所以这是我自己的备忘录。
如果有任何问题或建议,请不吝告知。

前提-只需一個選擇。

    • 開発環境でのRedisは問題なく動作すること

 

    • アプリケーションへのドメイン設定が完了していること

 

    • ElastiCacheの設定が完了していること

 

    .envを導入済みであること

使用版本

    • Ruby:2.6.5

 

    Ruby on Rails: 6.0.3

应用程序方面

Rails.application.routes.draw do
  # action cable
  mount ActionCable.server => '/cable'
end
REDIS_HOST="【ElastiCacheのエンドポイント】"
REDIS_PORT="6379"
Redis.new(:host => ENV['REDIS_HOST'], :port => ENV['REDIS_PORT'])
config.action_cable.disable_request_forgery_protection = true
config.action_cable.allowed_request_origins = ['https://【ドメイン名】, %r{https://【ドメイン名】.*}, 'http://【ドメイン名】', %r{http://【ドメイン名】.*}]
  config.session_store :redis_store, {
    :servers => {
        :host => ENV['REDIS_HOST'],
        :port => ENV['REDIS_PORT'],
        :db => 0,
        :namespace => 'sessions'
    },
    :expire_after => 1.days
  }
production:
  adapter: redis
  port: 6379
  url: redis://<%= ENV['REDIS_HOST'] %>

服务器端

nginx的配置

upstream unicorn_server {
    server unix:/var/www/projects/【アプリケーションディレクトリ】/tmp/sockets/.unicorn.sock
    fail_timeout=0;
}

server {
    listen 80;
    client_max_body_size 4G;
    server_name 【ドメイン名】;
    gzip on;

    keepalive_timeout 5;

    # Location of our static files
    root /var/www/projects/【アプリケーションディレクトリ】/public;

    location ~ ^/assets/ {
        root /var/www/projects/【アプリケーションディレクトリ】/public;
        location ~ .*.(js|css|html|svg)+$ {
            gzip_static on;
            gunzip on;
        }
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://unicorn_server;
            break;
        }
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /var/www/projects/【アプリケーションディレクトリ】/public;
    }

   location /cable {
        proxy_pass http://unicorn_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade websocket;
        proxy_set_header Connection Upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

bannerAds