8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails 6 のAPI を叩いたら "Blocked host: host.docker.internal" と怒られた

Last updated at Posted at 2019-10-05

構成

  • Docker
  • Rails 6 (APIモード)
  • Nuxt.js

エラーになった

Nuxt 側から Axios でRails のAPIを呼び出すぞ!
と今までと同じように以下の設定をしたけど

/.env
API_BASE_URL=http://host.docker.internal:8000
nuxt.config.js
  axios: {
    baseURL: process.env.API_BASE_URL
  },

こんなエラーが出てしまった🤔

image.png

Blocked host: host.docker.internal

To allow requests to host.docker.internal, add the following to your environment configuration:

config.hosts << "host.docker.internal"

解決法

色合い的に Railsのエラーっぽいなーと調べると、
Rails 6 で新たに追加された ActionDispatch::HostAuthorization というミドルウェアで、
許可していないホスト名宛のアクセスは拒否されるようになったみたい。
0.0.0.0localhost はデフォルトで許可されている)

Guard against DNS rebinding attacks by permitting hosts
https://github.com/rails/rails/pull/33145

エラーメッセージの通り、 config に config.hosts を追記して解決!

config/environments/development.rb
Rails.application.configure do
...
    config.hosts << "host.docker.internal"
end
8
5
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
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?