Pippi Life

主に仕事に関連するITのことや、プライベートもちょいちょい書きます。

Rails sで起動したサーバーにローカルPCから接続できない

Ruby on Railsを勉強中です。 いや、Rubyも書いたことないけど、まぁなんとかなるでしょ、と始めましたが、 環境作りで手間取っております。

とりあえずVagrant upで落としてきたイメージを起動するところは到達。

rails new

コマンドもたどり着いた。

で、

rails s

でhttpサーバーが起動されるところまできたのだけど、 ブラウザから接続できない。。。。

ipも間違ってない、ポートフォワーディングの設定もあっている。。。 なんかオプションか?とおもいつつ

rails s -h

と打ったら、--binding=IP を発見。

    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=IP                 Binds Rails to the specified IP.
                                     Default: localhost
    -c, --config=file                Uses a custom rackup configuration.
    -d, --daemon                     Runs server as a Daemon.
    -u, --debugger                   Enables the debugger.
    -e, --environment=name           Specifies the environment to run this server under (test/development/production).
                                     Default: development
    -P, --pid=pid                    Specifies the PID file.
                                     Default: tmp/pids/server.pid

    -h, --help                       Shows this help message.

このPCってlocalhostだよなと思いつつ、 ふとAWSのanywhereの表現の0.0.0.0にのっとって

rails s -b 0.0.0.0

にしてみたら、つながった。 PCはMacなので、ifconfigとかしてみたらわかるかと思ったけど、、

rails s -b en0のip
rails s -b vboxnet0のip

でも繋がらなかった。 まぁ動くからいいか。。。