在vmware 的虚拟机中安装了 CentOS7 ,并且安装了 ruby 2.2.1 和 rails 4.2.4

通过 rails s 启动 WEBrick ,出现以下问题:3000端口除了本机,其他机器无法访问(防火墙已经关闭)


=> Booting WEBrick
=> Rails 4.2.4 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-20 01:14:37] INFO  WEBrick 1.3.1
[2016-01-20 01:14:37] INFO  ruby 2.2.1 (2015-02-26) [x86_64-linux]
[2016-01-20 01:14:37] INFO  WEBrick::HTTPServer#start: pid=2482 port=3000


rails 只绑定了 localhost 的 3000 端口。这样就导致只能本地访问3000端口。其他机器无法访问,用telnet 192.168.1.158  3000 这种方式去查看端口状态,直接给拒绝。

出现这种问题的原因是 :


with rails 4.2.0, the server binds to localhost by default, instead of 0.0.0.0. When working with a rails in a virtual box, accessing the server from the host computer, the binding address needs to be 0.0.0.0

Start rails server with -b0.0.0.0 to make the rails server accessible from the host computer/browser.

http://guides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-serverhttps://github.com/samuelkadolph/unicorn-rails/issues/12#issuecomment-60875268


解决办法 :


使用 如下命令


rails s -b0.0.0.0

查看控制台信息,确定用0.0.0.0 来代替 localhost

[roamer@ror_server demo]$ rails s -b0.0.0.0
=> Booting WEBrick
=> Rails 4.2.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-20 01:21:05] INFO  WEBrick 1.3.1
[2016-01-20 01:21:05] INFO  ruby 2.2.1 (2015-02-26) [x86_64-linux]
[2016-01-20 01:21:05] INFO  WEBrick::HTTPServer#start: pid=2580 port=3000


Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐