GCP事始め。Compute EngineでRailsアプリを動かす で作成したサーバーでAction Cable(WebSocket)が動くようにしたのでメモしておきます。
Rails側は実装済みの状態です。Action CableでWebSocketを試す
今回は導入済みのNginxを使用しました。記事書いてくれている人も多いので迷わないですが。

Nginxの設定追加

こちらを参考にしました。
ただ、proxy_passの所だけ、/cable付けると上手くいかなかったので変更しています。
How to configure ActionCable with Nginx and Unicorn in production? – Stack Overflow

サーバーの/etc/nginx/conf.d/z_front.conf
※私の場合は、/etc/nginx/nginx.confからincludeしているので、このファイル。

    # Action Cable access
    location /cable {
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Host    $host;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_redirect      off;

        proxy_http_version  1.1;
        proxy_set_header    Upgrade             websocket;
        proxy_set_header    Connection          Upgrade;

        proxy_pass          http://rails-app-origin;
    }

Redis追加

サーバーへのRedisインストールはAnsible使っているの割愛。

Gemfile

# Use Redis(Use Action Cable)
gem 'redis', :group => :production
% bundle install

config/cable.yml は変更せずそのまま使用

development:
  adapter: async

test:
  adapter: test

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

config/application.rb(Action Cable実装時に設定済み)

    # Action Cable
    config.action_cable.mount_path = '/cable'
    config.action_cable.url = "#{Settings['websocket_url']}#{config.action_cable.mount_path}"
    config.action_cable.allowed_request_origins = Settings['websocket_allowed']

今回のコミット内容。
Nginxの設定もAnsibleで行っています。
https://dev.azure.com/nightonly/vagrant-ansible-origin/_git/vagrant-ansible-centos7/commit/3df783b9a18016692e20252ac68b83df5b55e35f

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です