1

I have a server running at localhost:8081 on the host machine.

From a VM, 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.

The host machine is running macOS 10.15.7 (Catalina) and the VM is running Ubuntu 18.04.

Is this possible? If so, how?

1 Answer 1

0

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:

<VirtualHost *:8081>

    ServerName localhost

    # Forward all request for "localhost" to another computer

    # Assuming the server on the host PC runs on port 80
    ProxyPass / http://1.2.3.4/
    ProxyPassReverse / http://1.2.3.4/

    # Assuming the server on the host PC runs on port 8081
    # ProxyPass / http://1.2.3.4:8081/
    # ProxyPassReverse / http://1.2.3.4:8081/

</VirtualHost>

Notes

  • 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. mod_proxy_http, mod_proxy_html) under /etc/apache2/mods-available on Ubuntu.
  • Under Ubuntu, virtual hosts should be placed in /etc/apache2/sites-available.
  • 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 ServerAlias and DocumentRoot should be unnecessary in this instance.
  • The IP addresses given above (i.e. 1.2.3.4) 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. http://some-local-name).
  • 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 ProxyPass and ProxyPassReverse directives.
  • 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .