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 networkWSLsudo apt update
sudo apt install openssh-server SSH service is up and runningsudo service ssh statusIf the service is not action then
startthe servicesudo service ssh start
ssh on bootSSH to start on bootsudo systemctl enable ssh --now ssh service statusservice sshd status wsl on windows to access from another devices on the same networkIP Address of wslip addrKeep 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 wslwf 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 WSLNginxWSL 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 localhostIf 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 --nowNginx 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 AddressFind the IP address of your WSL instance:
ip addror
hostname -IYou 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=80Replace <WSL-IP> with the IP address you found in the previous step. port forwarding rule:netsh interface portproxy show allIt should display:Listen on ipv4: Connect to ipv4:
0.0.0.0 80 172.28.x.x 80 FirewallAllow external traffic to port 80 on Windows:
Allow Nginx Port 80). Nginx on Windows:curl http://localhost browser and navigate to:http://localhostYou should see the default Nginx page. To allow access from other devices on the network:
IP address:ipconfigor 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!

Learn how to check and set your Git user.name and user.email for all projects (global) or a single repo (local). Fix your GitHub commit identity.

Stop fighting Git permissions in WSL. This post explains the root cause of the 'Permission Denied' error and shows you the permanent fix.

Learn why the Frappe framework's built-in reporting is a game-changer. See how to customize, group, and save reports instantly