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

DocumentRoot /var/www/megahost8080
ServerName megahost8080

Register IP

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

Relay port 8080 connections to IP

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

We need port forwarding because it won't work if configure our vhost to listen to 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:	megahost8080

Test it

Go to http://megahost8080:8080