LoginSignup
13
15

More than 5 years have passed since last update.

Vagrantで作成した仮想環境のRails4.2に繋がらない!!

Posted at

概要

Rails4.2の開発環境をVagrantのCentOS7で作成していました。
Rails実行環境が出来てbundle exec rails sで起動し、ホストOSのブラウザからIP指定してアクセスしようとしてもつながらず・・

色々試して解決できたので、記録しておきます。

前提条件

Vagrantで起動した仮想環境にホストOSのブラウザからIP指定でアクセスできるように、Vagrantfileに記載するネットワーク設定をpublic_networkにしています。

Vagrantfile
config.vm.network = "public_network"

解決方法

rails server のbinding オプションを設定する

Rails4.2でrails serverコマンドを実行すると以下のように表示されます。

$ bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-03-19 10:01:40] INFO  WEBrick 1.3.1
[2015-03-19 10:01:40] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
[2015-03-19 10:01:40] INFO  WEBrick::HTTPServer#start: pid=3487 port=3000

上記の3行目に,
Rails 4.2.0 application starting in development on http://localhost:3000
と記載されている通り、localhostからのアクセスしか受け取らないようになっています。

Rails4.2のリリースノートを確認してみるとしっかり記載されていますね・・・

3.3 rails serverのデフォルトホスト
Rackの変更 により、rails serverコマンドを実行した際のデフォルトのホストが0.0.0.0からlocalhostに変更されました。

VMで起動しているRailsサーバーに外のOSからIP指定でアクセウするには以下のようにbindingオプションで0.0.0.0を指定します。

$ bundle exec rails server -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-03-19 10:10:04] INFO  WEBrick 1.3.1
[2015-03-19 10:10:04] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
[2015-03-19 10:10:04] INFO  WEBrick::HTTPServer#start: pid=3571 port=3000

これで仮想VMのIPを指定してホストOSのブラウザからアクセスできるようになりました。

あとがき

ちゃんとリリースノートを読みましょう。

13
15
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
13
15