Не удается получить доступ к серверу rails на виртуальной машине с хоста

3275
Hubert Siwkin

У меня есть сервер rails на виртуальной машине, и мне нужен доступ к localhost с хоста. Я пробовал и NAT (переадресация портов - порт 80 и 3000 на гостевой стороне) и мостовой адаптер, но ничего не работает.

В обоих случаях мне удалось получить доступ к порту 80, но мне не удалось подключиться к локальному узлу через порт 3000 (поэтому не было проблем с доступом к серверу xampp).

Пожалуйста, дайте мне знать, если вы знаете, как можно получить доступ к localhost с хоста, пока сервер находится на гостевой стороне.

У меня Linux (Debian) на гостевой и Windows 7 на стороне хоста - я использую VirtualBox.

11

1 ответ на вопрос

9
Samuel Jaeschke

There are at least three separate components at play here, each of which need to be configured correctly.

Rails binding address

When Rails (or another server application) opens to receive network connections, it will bind to both an IP and port. If an incoming request doesn't match both of these, it won't accept the connection. From rails server --help:

Usage: rails server [mongrel, thin etc] [options] -p, --port=port Runs Rails on the specified port. Default: 3000 -b, --binding=IP Binds Rails to the specified IP. Default: localhost ... 

So by default, Rails will only accept requests sent to its localhost (loopback) network adapter (called lo). Since this adapter is only accessible from within the same computer, the only requests it will accept will be from within the Debian VM itself: good and secure for testing, but not useful for production, or even testing from a different computer.

If you want your Rails to be accessible from any other computer (including your Windows 7 host), you will need to tell Rails to bind to whichever IP address Debian has that is accessible from the outside (see below).

However, an easier way is to tell Rails to bind to the special IP address 0.0.0.0, which means any network adapter. So, starting your Rails server like so:

rails server -p 80 -b 0.0.0.0 

will make your website accessible to anyone who can see your VM on the network.

This is likely what has still messed you up even if you had the other two components working.

VM IP address

You need to point your browser at the Debian virtual machine, which is its own discrete computer.

The rule of thumb with localhost is that it always refers to the same computer. However, for networking purposes, your Debian VM is its own separate computer. If you're using a web browser in your Windows 7 host, localhost will always refer to your Windows 7 and nothing else.

You have two options here:

  • Set the VirtualBox VM adapter to Bridge mode (as you mentioned in your question). This will put your Debian directly on the same outside/home/work network as your Windows 7 host. You can then determine your Debian VM's IP by running ifconfig. Look for the IP of your eth0 or eth<something> adapter. Plug this into your browser's address bar.

  • Use a NAT adapter and VirtualBox port forwarding. IMO this is more complicated.

Debian firewall

It's been a while since I've worked on Debian so I don't know what the current defaults are, but if the above two things don't work then this might be worth looking into. If Debian is running a firewall, you'll need to open any ports you wish to access from another machine.

Hope this helps :)