LoginSignup
4
4

More than 3 years have passed since last update.

ActionCable + Nginx + Redisの設定

Posted at

はじめに

このごろSPAでフェッチというマッチングアプリを作っている阿部です。
マッチングアプリと言えばチャット。
ということでチャット機能を作りました。

下手にSPAなんかに手を出したので、
WebSocketを本番で使うのに、Redisが必須になってしまいました。

備忘録がてらActionCableをデプロイするための記事を書きました。

前提

Rails 5.2.3
Ruby 2.5.1
Puma
Nginx
Vue
Amazon Linux AMI release 2018.03

説明すること

  • Redisのインストール、実行
  • Rails側の設定
    • Production.rb
    • cable.yml
  • Nginxの設定
    • /etc/nginx/conf.d/*.conf

Redisのインストール、実行

サーバー上でコマンドを叩き、Redisをインストール

sudo yum install redis --enablerepo=epel

サーバー上でコマンドを叩き、Redisを実行

sudo service redis start

Rails側の設定

リクエスト元を設定してあげる。

Production.rb
  config.action_cable.allowed_request_origins = [ 'http://ドメイン', /http:\/\/ドメイン.*/]

WebSocket通信時のadapterを設定

Production.rb
development:
  adapter: async

test:
  adapter: async

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

Redisをstartした時のデフォルトのポートが6379の為、redis://localhost:6379。ポート変えたらここも要変更
channel_prefixは複数のアプリケーションでRedisをしようする為に必要らしい。一つのアプリケーションでしかRedisを使用しない場合は不要

Nginxの設定

/etc/nginx/conf.d/exemple.conf
    upstream puma-example-api {
        server unix:///home/user/htdocs/example/tmp/sockets/puma.sock;
    }

    location /cable {
        proxy_pass http://puma-example-api;
        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;
    }

参考

Action Cable の概要
Rails5のActionCableをCapistrano経由でデプロイ
Rails5のActionCableをNginxとPumaの環境にデプロイする

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4