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

To access a web server running inside a VirtualBox virtual machine from the host machine, you can use 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

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

  • 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 them 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

By entering this address in your browser, you can access the VM-hosted webserver from the host machine.

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 running inside the virtual machine.

Note: Make sure that the web server is correctly 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.