Search code examples
node.jsvirtualboxportforwarding

VirtualBox port forwarding issue


I'm having a strange issue with port forwarding. I've used it many times and it seems to work without issue for me (Ubuntu guest, Ubuntu host).

I'm now dealing with a problem where a specific NodeJS-based server will curl in the guest but not the host, despite port forwarding set on that port. I'm running another server on the same box (with a different port on both guest and host) which is working normally, so I know it's not a system problem with virtualbox, etc.

I've found numerous port forwarding questions and lots of solutions, but none of them have worked for me. I've checked that my firewall is not a problem. I've tried running both tcp and udp. I've tried changing the port from 3000 to 8123 which I know I have used successfully in the past. I've tried messing with almost every possible configuration of 0.0.0.0 and 127.0.0.1.

Basically, I'm out of ideas and looking for other things to try. What else could cause this type of issue? Are there any other debugging strategies to try?


Solution

  • Ok, I resolved the problem by setting the Express server to listen on an IP of 0.0.0.0 instead of 127.0.0.1.

    The port forwarding is for a particular network adapter. You'll note that in the settings, the port forwarding button is actually on the tab for a particular NIC.

    VirtualBox Network Settings

    When your code listens on 127.0.0.1, it's binding to the loopback virtual adapter only. It must bind to the network adapter you're port forwarding for. You can either bind to that interface's address directly, or use 0.0.0.0 to listen on any adapter. Once you've done that, your port forwarding to that particular adapter will work.