Skip to content

HTTP Server on WSL not accessible on W11 #9203

Closed

Description

@MGX-CODING

Version

Microsoft Windows [version 10.0.22000.1219]

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

5.10.16

Distro Version

Release: 20.04

Other Software

NodeJS          v16.11.1       # With NVM
NPM             8.0.0          # With NVM
@angular/cli    12.2.18        # NPM package

Repro Steps

  • Install Node through NVM on the WSL (or through "normal" means)
  • Select a folder of your choice
  • Run npx @angular/[email protected] new testproj
  • Wait for it to end
  • Once finished, run cd testproj
  • Run npm start

In a browser on Windows, go to http://localhost:4200

If page displays, no issue. If page not found, issue.

Expected Behavior

I would like to be able to access HTTP servers ran on WSL from my Windows OS.

Actual Behavior

I have to do major tweaks to make it work out of the box.

Diagnostic Logs

Up until 2 days ago, everything was working fine. I had no issue accessing the page.

But from nowhere, it started not working.

Now I have to run a custom script to create NAT/PAT/Firewall rules on Powershell :

  $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
  $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

  if ( $found ) {
    $remoteport = $matches[0];
  }
  else {
    echo "The Script Exited, the ip address of WSL 2 cannot be found";
    exit;
  }

  #[Ports]

  #All the ports you want to forward separated by coma
  $ports = @(3000, 4200);


  #[Static ip]
  #You can change the addr to your ip config to listen to a specific address
  $addr = '0.0.0.0';
  $ports_a = $ports -join ",";


  #Remove Firewall Exception Rules
  iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

  #adding Exception Rules for inbound and outbound Rules
  iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
  iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

  for ( $i = 0; $i -lt $ports.length; $i++ ) {
    $port = $ports[$i];
    iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
    iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
  }

And once this script is ran, I have to run the following command to make it work :

npm start -- --host=0.0.0.0

The issue started to appear really out of nowhere, the last WSL command I ran before getting the issue was

eval `ssh-agent`

Between the no issue and issue state, no update has been made, the computer or the WSL has not been restarted, it just happened out of the blue with no reason.

Activity

  1. MGX-CODING commented on Nov 24, 2022

    @MGX-CODING
    Author

    I should also state that I disabled fast startup without success, and I would like NOT to rewrite my hosts files, which seems to be kind-of-working solutions that I have found online.

    Juste running the PS1 script mentioned above is already a pain in the ass, my computer having to restart everyday. And yes, I know I can create sort of a CRON task to run it, but the thing is, I shouldn't have to, since it was working 2 days prior.

  2. AbhinasRegmi commented on Nov 25, 2022

    @AbhinasRegmi

    May be you need to reset your port proxy. I faced similar problem of not being able to access my
    django webserver from windows OS. I ran the following command and restarted my machine.
    Everything worked fine afterwards. Run this in Powershell.
    netsh interface portproxy reset

  3. MGX-CODING commented on Nov 28, 2022

    @MGX-CODING
    Author

    Hi, sorry for the late reply, been a busy week-end.

    Abhinas Regmi (@AbhinasRegmi) nope, did not work. Ran the command in Powershell, and restarted my WSL machine as well as my Windows machine, still getting the issue.

  4. MGX-CODING commented on Nov 28, 2022

    @MGX-CODING
    Author

    It's even worse now, I cannot access my app anymore, even with the PS1 script mentioned above. How do you revert this command ?

    EDIT : OK nevermind I got it back by restarting the WSL machine again

  5. MGX-CODING commented on Nov 29, 2022

    @MGX-CODING
    Author

    Update : for some reason I started to work again after another restart of my computer.

    I suspect your command did the trick Abhinas Regmi (@AbhinasRegmi), it just had to be restarted twice (don't ask me why because I absolutely have no idea ...)

    Thank you for the trick anyways !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions