Help us understand the problem. What is going on with this article?

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
tomoeine
宮崎の山の中でリモートフリーランス🌴 Laravel, Rails, Vue.js など。 宮崎のWeb系フリーランス集団"てげほげ"所属で勉強会の企画・運営してます。宮崎のエンジニア盛り上げたい💪 ゆるくスケジュール共有するゆるすけ→https://yurusuke.com
https://yurusuke.com
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした