10 December 2012

Accessing server vhosts on the VirtualBox guests

This post will probably help you, if you are searching for way to access an Apache/Nginx vhost on VirtualBox's guest. Let's assume we have vhost mega8080 listening on port 8080. The following makes it available for Windows XP guest.

Edit vhost configuration

Vhost configuration file like /etc/apache2/sites-available/localhost should listen to port 8080:

ServerName server.megahost
NameVirtualHost 127.0.0.3:8080


DocumentRoot /var/www/megahost8080
ServerName megahost8080

Register IP

In our sample /etc/hosts should have the following like:

127.0.0.3	megahost8080

Relay port 8080 connections to IP 127.0.0.3

# iptables -t nat -I PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 127.0.0.3
# iptables -t nat -A OUTPUT -p tcp --dport 8080 -j DNAT --to-destination 127.0.0.3
We need this because in Windows XP ipconfig outputs:
IPv4Address: 10.0.2.15
Default Gateway: 10.0.2.2

We need port forwarding because it won't work if configure our vhost to listen to 10.0.2.15 directly.

Simplest way to configure network for guest is to keep it's default NAT configuration :)

Network adapter: 
 - attached to: NAT 
 - type: PCnet-FAST III (NAT)

Forward port 8080 connections through TCP in VBox

$ VBoxManage setextradata "winxp" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol" TCP
$ VBoxManage setextradata "winxp" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/GuestPort" 8080
$ VBoxManage setextradata "winxp" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/HostPort" 8080
Or, in new versions, just set it up via GUI: Settings - Network - Port Forwarding.

Bind guest IP to the hostname

File C:\windows\system32\drivers\etc\hosts(or similar) should contain line like the following:

10.0.2.2	megahost8080

Test it

Go to http://megahost8080:8080