Unable to access Sinatra app on host machine with

2019-03-27 06:02发布

问题:

After starting my Sinatra application with both ruby app.rb and foreman start I am unable to access my application with localhost and the respective port on my host machine. I am also able to curl to the applications from within the shell of on guest machine, whereas on the host machine the curl request fails. As far as I know there shouldn't be a firewall in place on the guest machine because I'm using the Vagrant Ubuntu image.

My Vagrantfile is as follows:

Vagrant.configure('2') do |config|
  config.vm.box = 'precise32'
  config.vm.box_url = 'http://files.vagrantup.com/precise32.box'
  config.vm.network :forwarded_port, guest: 4567, host: 4567
end

回答1:

By default when running in development mode, Sinatra only listens to localhost, not to 0.0.0.0 (this change was made due to security considerations).

To allow requests from any interface, either add set :bind, '0.0.0.0' to your app file, or start your app with the -o option, e.g. ruby myapp.rb -o 0.0.0.0.

You may be able to set this to the actual address assigned to the guest, but I don’t know if it will be worth it.