0
0

【Rails6】[ActionDispatch::HostAuthorization::DefaultResponseApp] Blocked hosts:の対処法

Posted at

問題

Rails6で任意のhost名でアクセスしようとした時に、以下のエラーがでます。

[ActionDispatch::HostAuthorization::DefaultResponseApp] Blocked hosts: example.com

解決方法

Rails6で追加された、ActionDispatch::HostAuthorizationによるものです。
https://github.com/rails/rails/pull/33145

対応方法は以下の3つがあります。

1. Rails.application.config.hostsにhostを追加する

development環境では、デフォルトで以下のhostが登録されています。
これ以外で接続しようとするとブロックされるため、必要なhost名を追加します。

Rails.application.config.hosts = [
  IPAddr.new("0.0.0.0/0"), # All IPv4 addresses.
  IPAddr.new("::/0"),      # All IPv6 addresses.
  "localhost"              # The localhost reserved domain.
]

以下のようにconfig/environments/development.rbに追加します。

Rails.application.configure do
  config.hosts << "example.com"
end

2. Rails.application.config.hostsの設定をクリアする

config.hostsを空にするとブロックされなくなります。
以下のようにconfig/environments/development.rbに追加します。

Rails.application.configure do
  config.hosts.clear
end

3. ActionDispatch::HostAuthorizationを使わない

config/application.rbに以下のように記述します。

module TestApp
  class Application < Rails::Application
    config.middleware.delete ActionDispatch::HostAuthorization
  end
end

参考

0
0
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
0
0