How to access the VirtualBox web server from the host using VBox port forwarding

You can access a web server inside a VirtualBox virtual machine from the host machine using VBox port forwarding. Port forwarding allows you to redirect network traffic from specific ports on the host machine to corresponding ports on the guest virtual machine.

VirtualBox Port forward configuration

Here are the steps if you are looking for a way to access/connect to the web server running inside your Ubuntu VirtualBox VM from a Windows host.;

  • Run the web server in Ubuntu VM (apache or Nginx)
  • Identify the port of the web server – by default, it is PORT 80
  • From VirtualBox host – VM settings, enable port forwarding

Let us do it in straightforward three steps

Related: How to attach a VM to the VirtualBox NAT network?

Step #1 Start and run the webserver in the VM

First, ensure that the webserver is running in the VM. You may verify this by accessing the URL: localhost from a browser inside the VM

Or using the command:

# For Apache2
sudo systemctl status apache2.service

If in case you have Nginx running

# For Nginx
sudo service nginx status

Once you are sure that the website is up and running, proceed to Step #2

Step #2 How to enable port forwarding in VirtualBox VM

We are going to forward the Host machine’s ( your laptop’s ) PORT 80 to the VirtualBox VM’s PORT 80

For this, follow the below configuration steps

  • Open VirtualBox VM -> Settings
  • Go to Network -> Attached to NAT
  • Expand Advanced and enable checkbox – Cable Connected
  • Click on Port Forwarding
  • Doubleclick and enter the value 80 under Host PORT and Guest PORT, as shown here:

Save your settings by clicking OK.

Step #3 Accessing the URL from your host’s web-browser

Now open your host machine browser and enter localhost as the URL

Now, you should be seeing your Ubuntu-hosted webserver from your Windows Host!

Notes:

This setup works, even if your machine is not connected to the internet (or has an external network).

You may also access the web server using a domain alias by entering it in your Windows host file. The host file is located at:

C:\Windows\System32\drivers\etc\hosts

Make a new line entry for the test-domain name, such as for code.codetryout.com:

127.0.0.1   code.codetryouot.com

You can access the VM-hosted webserver from the host machine by entering this address in your browser.

Conclusion

The host machine will forward any traffic received on the specified host port to the guest virtual machine’s IP address and the configured guest port, allowing you to access the web server inside the virtual machine.

Note: Ensure the web server is configured inside the guest virtual machine and listening on the specified guest port. Additionally, ensure that any firewalls on the host or guest are not blocking the traffic.