在vmwate的CentOS7的虚拟机中安装在rails 4.2.0 版本以后,无法通过其他机器访问 3000端口的问题

本文介绍了解决Rails服务器仅绑定到localhost的问题,通过将服务器绑定地址更改为0.0.0.0,使虚拟机中的Rails应用能够被主机或其他网络设备访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值