A simple HTTP proxy server that forwards requests to a Node.js server running on a WSL2 instance.
If you're running a Node.js server in WSL2 and want to access it from another machine on your network, you may run into issues due to the default network configuration. This should work for the default "NAT" mode as WSL2 instances are assigned a private IP address, and incoming connections from the network are not directly forwarded to them.
Manually configure a static IP address for your WSL2 instance, which would allow you to connect to your Node.js server using that IP address. If you prefer to take this approach, you can find instructions online for how to configure a static IP address in WSL2.
- Clone this repository to your host machine.
- Start your Node.js server inside your WSL2 instance.
- In a terminal, navigate to the cloned repository.
- Run
npm installto install the required dependencies.
-
Start the proxy server by running the following command:
node proxy.jsThis automatically detects wsl instances and node sever ports and prompts the user to select which instance and ports to use
-
In a web browser or other HTTP client, navigate to
http://localhost:<PROXY_PORT>/orhttp://<WINDOWS_LOCAL_IP>:<PROXY_PORT>/to access your Node.js server running in the WSL2 instance.You shouldn't need to bind the WSL2 node server to
0.0.0.0or<WSL2_IP>in order for this to work. Default config should be fine.
I've updated this repo so that you don't have to manually enter the IP address of your WSL2 instance (as this seems to change on reboot), the port your Node.js server is listening on, and the port you want to listen on for incoming HTTP requests using proxy.js
If you have issues with this you can instead use proxy-basic.js
-
Start the proxy server by running the following command:
node proxy-basic.js(if you have defined the variables in the proxy-basic.js file)
const addr = process.argv[2] || "172.24.76.254"; // the IP of your WSL2 instance const target_port = process.argv[3] || 7070; // the port your node.js server is listening on const port = process.argv[4] || 6969; // the port you want to listen on for incoming HTTP requests
or
node proxy-basic.js <WSL2_IP> <WSL2_PORT> <PROXY_PORT>where
<WSL2_IP>is the IP address of your WSL2 instance,<WSL2_PORT>is the port your Node.js server is listening on in the WSL2 instance, and<PROXY_PORT>is the port you want to listen on for incoming HTTP requests on the host machine.
in your WSL2 instance, run the following command:
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1
this will return the IP address of your WSL2 instance.
172.23.08.34
if this doesn't work because the network adapter isn't on eth0, you can also run the following command:
ip addr show
look for the address listed after inet. Example:
6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether .......
inet 172.23.08.34/20 brd ....... scope global eth0
valid_lft forever preferred_lft forever
inet6 ......... scope link
valid_lft forever preferred_lft foreverin this example the WSL2 IP address is 172.23.08.34
To access the Node.js server running on the WSL2 instance, you may need to open the appropriate ports in Windows Firewall and/or in the UFW firewall on the WSL2 instance.
-
Open Windows Firewall with Advanced Security.
-
Click
Inbound Rulesand thenNew Rule. -
Choose the
Portoption and clickNext. -
Select the appropriate protocol and enter the port number.
-
Choose the
Allow the connectionoption and clickNext. -
Select the appropriate network profiles and click
Next. -
Enter a name and description for the rule and click
Finish.
-
Open the terminal on your WSL2 instance.
-
Run the following command to allow incoming connections on the appropriate port:
sudo ufw allow <port_number>/tcp
Replace <port_number> with the port number that your Node.js server is listening on.
