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

Access WSL Nginx Web Server

Access WSL Nginx Web-Server from other devices on the same network

port forwarding nginx on windows server setup nginx wsl WSL wsl configuration NGINX network access Windows Subsystem for Linux web development web-server configure nginx windows firewall

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

  1. Ensure WSL is installed, and you have an Ubuntu distribution set up.
  2. Set password for root and create a new user

Access wsl from another device on the same network

Install OpenSSH

  1. Install OpenSSH on WSL
    sudo apt update
    sudo apt install openssh-server
  2. Check if the SSH service is up and running
    sudo service ssh status

    If the service is not action then start the service sudo service ssh start

Enable ssh on boot

  1. Enable SSH to start on boot
    sudo systemctl enable ssh  --now
  2. Check the ssh service status
    service sshd status

Configure wsl on windows to access from another devices on the same network

  1. Find the IP Address of wsl
    ip addr

    Keep a note of the IP address, it will be required for the next step

  2. Open a new Windows Terminal with administrator privileges
    netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=[wsl_ip_address]
  3. 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

  1. Run wf to open Windows Advanced firewall
    1. Add a new inbound rule, using the same IP address that we are listening to, in this case, it is port 22
  2. Find the IP address of the Windows Machine
    ipconfig

Access the wsl from another device on the same network

  1. Now from another device with the same network SSH using the pc IP address with the username of wsl
    ssh userName_or_root@[pc_ip_address]

2. Set Up Nginx in WSL

Install Nginx

  1. Open your WSL terminal.
  2. 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

  1. Check if Nginx is running:
    sudo service nginx status
  2. 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.

wsl-nginx-localhost-now-acessable-from-windows-browser

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:

  1. Open the Nginx configuration file:
    sudo nano /etc/nginx/sites-enabled/default
  2. Look for the server block and update the listen directive:
    server {
        listen 0.0.0.0:80;
        server_name localhost;
        root /var/www/html;
        index index.html;
    }
  3. 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:

  1. Open Command Prompt as Administrator on Windows.
  2. Add a 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.
  3. Verify the 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

6. Configure Windows Firewall

Allow external traffic to port 80 on Windows:

  1. Open Windows Defender FirewallAdvanced Settings.
  2. Go to Inbound RulesNew Rule:
    • Select PortTCP → Enter 80.
    • Choose Allow the connection → Apply to all profiles.
    • Name it (e.g., Allow Nginx Port 80).

7. Test Locally

From Windows:

  1. Test Nginx on Windows:
    curl http://localhost
  2. Open a browser and navigate to:
    http://localhost
    You should see the default Nginx page.

8. Test from Another Device on the Same Network

To allow access from other devices on the network:

  1. Find the Windows host's IP address:
    ipconfig
    or
    ipconfig | findstr "IPv4"
    Look for the IPv4 Address under your active network adapter.
  2. From another device on the same network, open a browser and enter:
    http://<Windows-IP>
    Replace <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 Nginx Logs: In 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!