How to Configure Nginx in WSL to Access from other devices on the same network

This guide explains how to set up Nginx
in WSL
(Windows Subsystem for Linux
) so it can be accessed from both Windows and other devices on the same network.
Ubuntu
Distro on WindowsWSL
is installed, and you have an Ubuntu
distribution set up. root
and create a new user
wsl
from another device on the same networkWSL
sudo apt update
sudo apt install openssh-server
SSH
service is up and runningsudo service ssh status
If the service is not action then
start
the servicesudo service ssh start
ssh
on boot
SSH
to start on boot
sudo systemctl enable ssh --now
ssh
service statusservice sshd status
wsl
on windows
to access from another devices on the same networkIP Address
of wsl
ip addr
Keep a note of the
IP address
, it will be required for the next step
Windows Terminal
with administrator
privilegesnetsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=[wsl_ip_address]
netsh interface portproxy show all
windows
firewall to access the wsl
wf
to open Windows Advanced firewallIP address
that we are listening to, in this case, it is port 22
IP address
of the Windows Machineipconfig
wsl
from another device on the same networkSSH
using the pc IP address
with the username
of wslssh userName_or_root@[pc_ip_address]
Nginx
in WSL
Nginx
WSL
terminal. Nginx
:sudo apt update
sudo apt install nginx -y
-y
: This flag automates the installation process by automatically answering "yes" to any questions that might arise during the installation
Nginx
is running:sudo service nginx status
sudo service nginx start
Nginx
should be running on localhost
If you visit http://localhost
in the browser
on your Windows
machine, you should see the default Nginx
welcome page.
Nginx
on BootEnsure Nginx
starts automatically when WSL
is started:
sudo systemctl enable nginx --now
Nginx
to Listen on All InterfacesBy default, Nginx
listens only on localhost
. Update its configuration to listen on all interfaces:
sudo nano /etc/nginx/sites-enabled/default
server
block and update the listen
directive:server {
listen 0.0.0.0:80;
server_name localhost;
root /var/www/html;
index index.html;
}
Nginx
:sudo service nginx restart
WSL
IP Address
Find the IP address
of your WSL
instance:
ip addr
or
hostname -I
You will see something like 172.28.x.x
. Note this IP
for the next step.
To make Nginx
accessible from other devices (including Windows machine
, macOS
, android
, ios
, linux
) on the same network via http://localhost
:
Command Prompt
as Administrator
on Windows
. port
forwarding rule:netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=80 connectaddress=<WSL-IP> connectport=80
Replace <WSL-IP>
with the IP address
you found in the previous step. port
forwarding rule:netsh interface portproxy show all
It should display:Listen on ipv4: Connect to ipv4:
0.0.0.0 80 172.28.x.x 80
Firewall
Allow external traffic to port 80
on Windows
:
Allow Nginx Port 80
). Nginx
on Windows
:curl http://localhost
browser
and navigate to:http://localhost
You should see the default Nginx
page. To allow access from other devices on the network:
IP address
:ipconfig
or ipconfig | findstr "IPv4"
Look for the IPv4 Address
under your active network adapter. browser
and enter:http://<Windows-IP>
Replace <Windows-IP>
with your Windows machine's IP. 80
is allowed. netsh interface portproxy show all
displays the correct forwarding. WSL
, inspect the logs for errors
:sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
By following these steps, you can configure Nginx on WSL to be accessible both from Windows and from other devices on the same network. Let me know if you have any issues!