WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Cannot access http server located in Windows from WSL2

Open alshdavid opened this issue 6 years ago • 14 comments

Please fill out the below information:

  • Your Windows build number: (Type ver at a Windows Command Prompt) Microsoft Windows [Version 10.0.19041.264]

  • WSL Details: WSL2 Ubuntu 20.04

  • What you're doing and what's happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screen shots if that helps demonstrate the problem.)

I am unable to access a web server located in Windows from inside WSL2.

Below I will create an http server, run it inside a Windows Command Prompt then attempt to curl it from WSL2.

I'm not familiar with an easy way to spin up an http server from Windows so I will use Go to write a tiny http server. I'm sure there is a better way, so feel free to use whatever you'd like to.

Video: output

Open a powershell window

mkdir server
cd server
touch main.go

Paste the following code into main.go

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "hello world\n")
	})
	http.ListenAndServe(":8090", nil)
}

Then in the powershell window run:

go run main.go

Accept the firewall prompt Verify your server is working by opening Edge browser and navigating to http://localhost:8090

You can also verify it works by using curl form another Windows Command Prompt

Now open a new terminal window with WSL2 and Ubuntu 20.04

curl http://localhost:8090

We would expect to see Hello World but instead we see:

curl: (7) Failed to connect to localhost port 8090: Connection refused

** Use cases **

Puppeteer cannot be accessed from WSL https://github.com/puppeteer/puppeteer/issues/5957

alshdavid avatar Jun 02 '20 06:06 alshdavid

For visibility, users can access the IP of the wsl host using the following line

tail -1 /etc/resolv.conf | cut -d' ' -f2

The http server must also be given access to public networks

image

Then running the following commands inside WSL2 will allow access to the http server.

export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)
curl http://$WSL_HOST:8090

This does not solve the issue of puppeteer because Chrome would need to be granted similar firewall permissions and also is hardcoded to only accept connections from localhost

alshdavid avatar Jun 02 '20 07:06 alshdavid

This direction (wsl -> windows) looks unsupported yet: I have the same issue.

bobvandevijver avatar Jun 02 '20 08:06 bobvandevijver

Since you're using WSL 2, you'll need to access your Windows networking applications using the IP of your host machine. We're investigating adding 'localhost' support to this in the future!

Instructions on how to do that are here. Let me know if that works for you!

craigloewen-msft avatar Jun 04 '20 16:06 craigloewen-msft

@craigloewen-msft I found that page, but interestingly, /etc/resolv.conf does not contain the IP of the host machine, but a private network IP (172.21.xx.1) which doesn't respond to anything.

Using the actual IP of the machine does work, but that is something that changes all the time for me... Without a way of retrieving it from the WSL2 machine, I cannot use it consistently. For now, I downgraded to WSL1.

Edit: I'm opening a separate issue for that.

bobvandevijver avatar Jun 04 '20 18:06 bobvandevijver

I was able to get response from Windows server after I did this:

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

kilork avatar Jun 24 '20 06:06 kilork

Since you're using WSL 2, you'll need to access your Windows networking applications using the IP of your host machine. We're investigating adding 'localhost' support to this in the future!

Instructions on how to do that are here. Let me know if that works for you!

While this does generally work as a workaround, there are certain applications which validate that the client is running on localhost - such as when trying to debug Chrome through a tool like "puppeteer"

alshdavid avatar Jun 24 '20 07:06 alshdavid

From puppeteer/puppeteer/#2242 it looks like (?) it will accept *.localhost names which you can set up with avahi-daemon(8) or an alias in /etc/hosts. Other option is to run chromium and puppeteer in WSL with an X Server.

Localhost in the windows direction is /dupe #4619.

therealkenc avatar Jun 24 '20 16:06 therealkenc

Hi! We've identified this issue as a duplicate of another one that already exists in this repository. This specific instance is being closed in favor of tracking the concern over on the referenced thread.

Thanks for your report!

ghost avatar Jun 24 '20 16:06 ghost

avahi-daemon(8)

Sadly this will not work because Chrome does the validation. The proxy needs to live on the Windows side as the "remote client" is the address of the WSL guest.

I don't mind working around this, but I'd need to write a readme for my team to follow and I can't guarantee everyone can

alshdavid avatar Jun 28 '20 23:06 alshdavid

Sadly this will not work

Will not or did not.

therealkenc avatar Jun 29 '20 00:06 therealkenc

This Microsoft page helped solve this on my end

https://docs.microsoft.com/en-us/windows/wsl/networking

Check in the "Accessing Windows networking apps from Linux (host IP)" section

doctorchuks avatar Aug 22 '22 11:08 doctorchuks

try:

http.ListenAndServe("127.0.0.1:8090", nil)

godot42x avatar Apr 03 '23 12:04 godot42x

try:

http.ListenAndServe("127.0.0.1:8090", nil)

This worked for me! Thanks!

HttpRouter was running fine on MacOS. Transfered it to WSL and the exe would just start, and then exit, without any error message. But changing it to the above worked.

MiCurry avatar May 30 '23 00:05 MiCurry

I ran the firewall commands, but I don't know if they did anything. What definitely worked for me was to grab the IP address from here:

tail -1 /etc/resolv.conf | cut -d' ' -f2

Is this in some documentation somewhere? I got lucky to find it in this thread...

brunolm avatar Nov 08 '23 20:11 brunolm