在虚拟机上托管rails服务器

时间:2017-02-25 23:45:02

标签: ruby-on-rails virtual-machine virtualbox host

我目前正在Mac OS上安装Debian虚拟机,我已经安装了Ruby 2.4.0。我正在使用 Virtualbox和Vagrant

首先,我无法以这种方式启动我的服务器 rails server,因为当我尝试在Mac OS网络浏览器上访问它时,我有这样的错误:

The localhost page isn’t working

localhost didn’t send any data.

所以,我必须以这种方式启动它:rails server -b 0.0.0.0我想知道为什么我无法在127.0.0.1上启动它(默认IP)

此外,这是我启动Rails服务器时收到的消息。

/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-

5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Puma starting in single mode...
* Version 3.7.1 (ruby 2.4.0-p0), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

虽然我知道弃用的东西警告与我使用最新版本的Ruby有关,但我不理解最后5行:

Puma starting in single mode...
* Version 3.7.1 (ruby 2.4.0-p0), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
你可以解释一下这些的含义吗?

最后但同样重要的是,当我去http://0.0.0.0:3000时,即使我有正确的显示(耶!你在轨道上!)我在控制台上有这个奇怪的消息。

Started GET "/" for 10.0.2.2 at 2017-02-25 23:42:38 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML
  Parameters: {"internal"=>true}
  Rendering /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/templates/rails/welcome/index.html.erb (1.6ms)
Completed 200 OK in 10ms (Views: 4.2ms | ActiveRecord: 0.0ms)

您能否解释一下如何解决此问题:Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

谢谢!

1 个答案:

答案 0 :(得分:2)

在同一台计算机上运行的其他应用程序可以访问在任何计算机上的localhost接口上运行的应用程序。在您的情况下,MacOS(主机)和Vagrant(客户)框是两个不同的机器。因此,无法从主机访问绑定到Vagrant框上的localhost接口的应用程序。

使用rails s运行rails应用时,rails将绑定到接口localhost,如下所示

ubuntu@ubuntu-xenial:~$ netstat -an |grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN

在Vagrant框中运行的其他应用程序可以访问rails应用程序。

另一方面,如果使用rails s -b 0.0.0.0运行rails应用程序,rails app将绑定到所有接口,如下所示。

ubuntu@ubuntu-xenial:~$ netstat -an |grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN

使用-b 0.0.0.0运行rails应用程序会打开从主机访问您的应用程序的权限。

但是,在能够访问rails应用程序之前,需要做更多的事情。您的Vagrantfile中似乎已经有条目了。但是,无论如何我都在这里添加它。

转发所需的端口,如下面的Vagrantfile中所示。

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 3000, host: 8000
  config.vm.network "forwarded_port", guest: 3001, host: 8001
  config.vm.network "forwarded_port", guest: 3002, host: 8002
end

可以使用http://localhost:8000访问在Vagrant框的端口3000上运行的rails应用程序。

可以通过在Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

中添加此行来停用config/environments/development.rb消息
config.web_console.whitelisted_ips = '10.0.2.2'