Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@tomoeine

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

構成

  • 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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?