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.
1. Install Ubuntu
Distro on Windows
- Ensure
WSL
is installed, and you have anUbuntu
distribution set up. - Set password for
root
and create anew user
Access wsl
from another device on the same network
Install OpenSSH
- Install OpenSSH on
WSL
sudo apt update sudo apt install openssh-server
- Check if the
SSH
service is up and runningsudo service ssh status
If the service is not action then
start
the servicesudo service ssh start
Enable ssh
on boot
- Enable
SSH
to start onboot
sudo systemctl enable ssh --now
- Check the
ssh
service statusservice sshd status
Configure wsl
on windows
to access from another devices on the same network
- Find the
IP Address
ofwsl
ip addr
Keep a note of the
IP address
, it will be required for the next step - Open a new
Windows Terminal
withadministrator
privilegesnetsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=[wsl_ip_address]
- To show the interface port proxy in the Windows host machine run the following command
netsh interface portproxy show all
Configure windows
firewall to access the wsl
- Run
wf
to open Windows Advanced firewall- Add a new inbound rule, using the same
IP address
that we are listening to, in this case, it is port22
- Add a new inbound rule, using the same
- Find the
IP address
of the Windows Machineipconfig
Access the wsl
from another device on the same network
- Now from another device with the same network
SSH
using the pcIP address
with theusername
of wslssh userName_or_root@[pc_ip_address]
2. Set Up Nginx
in WSL
Install Nginx
- Open your
WSL
terminal. - Update your package manager and install
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
Start Nginx
- Check if
Nginx
is running:sudo service nginx status
- If it's not running, start the service:
sudo service nginx start
At this point 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.
Enable Nginx
on Boot
Ensure Nginx
starts automatically when WSL
is started:
sudo systemctl enable nginx --now
3. Configure Nginx
to Listen on All Interfaces
By default, Nginx
listens only on localhost
. Update its configuration to listen on all interfaces:
- Open the Nginx configuration file:
sudo nano /etc/nginx/sites-enabled/default
- Look for the
server
block and update thelisten
directive:server { listen 0.0.0.0:80; server_name localhost; root /var/www/html; index index.html; }
- Save the file and restart
Nginx
:sudo service nginx restart
4. Find Your 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.
5. Set Up Port Forwarding on Windows
To make Nginx
accessible from other devices (including Windows machine
, macOS
, android
, ios
, linux
) on the same network via http://localhost
:
- Open
Command Prompt
asAdministrator
onWindows
. - Add a
port
forwarding rule:
Replacenetsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=80 connectaddress=<WSL-IP> connectport=80
<WSL-IP>
with theIP address
you found in the previous step. - Verify the
port
forwarding rule:
It should display:netsh interface portproxy show all
Listen on ipv4: Connect to ipv4: 0.0.0.0 80 172.28.x.x 80
6. Configure Windows Firewall
Allow external traffic to port 80
on Windows
:
- Open Windows Defender Firewall → Advanced Settings.
- Go to Inbound Rules → New Rule:
- Select Port → TCP → Enter 80.
- Choose Allow the connection → Apply to all profiles.
- Name it (e.g.,
Allow Nginx Port 80
).
7. Test Locally
From Windows:
- Test
Nginx
onWindows
:curl http://localhost
- Open a
browser
and navigate to:
You should see the defaulthttp://localhost
Nginx
page.
8. Test from Another Device on the Same Network
To allow access from other devices on the network:
- Find the Windows host's
IP address
:
oripconfig
Look for theipconfig | findstr "IPv4"
IPv4 Address
under your active network adapter. - From another device on the same network, open a
browser
and enter:
Replacehttp://<Windows-IP>
<Windows-IP>
with your Windows machine's IP.
9. Troubleshooting
- If Not Accessible:
- Check Windows Firewall rules and ensure port
80
is allowed. - Verify
netsh interface portproxy show all
displays the correct forwarding.
- Check Windows Firewall rules and ensure port
- Check Nginx Logs:
In
WSL
, inspect the logs forerrors
: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!