LoginSignup
12
2

More than 3 years have passed since last update.

Railsでforce_ssl設定したらヘルスチェックが通らなくなった時の対処

Posted at

問題

ある日、AWS上で運営しているRailsのセキュリティを強化するためにforce_sslの設定を行ったのですが、その後ELBからのステータスがunhealthyとなってしまいました。

対応

おそらくヘルスチェックがHTTP(80番)で来ているからだと思われます。
そこでELBで設定したヘルスチェック用のアクセス(例 /health)は除外するように設定します(Rails5以降)

config/environments/production.rb
   config.force_ssl = true
+  config.ssl_options = { redirect: { exclude: -> request { request.path =~ /health/ } } }

こうすれば無事ステータスがHealthyとなりました。

おまけ

ヘルスチェック用のコントローラーとアクションを追加しても良かったのですが、少しでも負荷を減らしたかったのでルーティング側で直接応答を返すようにしてみました。

routes.rb
get 'health' => lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
12
2
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
12
2