I would like to be able to visit
http://localhost:8081 from a browser running inside of my VM and have that request forwarded to the server running on the host.
A basic issue that you are likely to run into is the browser itself automatically mapping the name localhost to the loopback address
127.0.0.1, which references the "local" computer (i.e. your VM, not the host PC).
One possible way to bypass this is behavior is to run another webserver (e.g. Apache) on the VM and have it forward requests for
localhost to the host PC.
As an example, with Apache on your VM, you can enable its proxy functionality (via mod_proxy) and then add a virtual host similar to the following:
# Forward all request for "localhost" to another computer
# Assuming the server on the host PC runs on port 80
ProxyPass / http://18.104.22.168/
ProxyPassReverse / http://22.214.171.124/
# Assuming the server on the host PC runs on port 8081
# ProxyPass / http://126.96.36.199:8081/
# ProxyPassReverse / http://188.8.131.52:8081/
- You will likely need to change
Listen 80 to
Listen 8081 under
/etc/apache2/ports.conf on Ubuntu.
- You should be able to enable
mod_proxy and any additional required modules (e.g.
/etc/apache2/mods-available on Ubuntu.
- Under Ubuntu, virtual hosts should be placed in
- Be aware that with name-based virtual hosts, ordering of the hosts may matter (though this seems unlikely to be a concern in your case).
- The common Apache virtual host directives
DocumentRoot should be unnecessary in this instance.
- The IP addresses given above (i.e.
184.108.40.206) are examples. You should replace them with the IP your VM uses to communicate with the host PC or other valid host names your VM can resolve (e.g.
- The use of a trailing
/ after the e.g. IP addresses above is required when proxying
- For simplicity, you should avoid using HTTPS in your
- Any time you make configuration changes to Apache, it should be restarted to have those changes take effect.
- Firewalls could still be an issue.
- If you don't care for Apache, Nginx is another another webserver with similar options.
Official documentation for setting up Apache on Ubuntu (including enabling virtual hosts, modules and restarting Apache) is available here.