Linux – Windows 10 WSL cURL can’t access localhost with error (Failed to connect to localhost port 80: Connection refused)

curllocalhostwindowswindows 10windows-subsystem-for-linux

The issue with my WSL is the following my WSL can't make a cURL request anywhere under localhost. For this example, I have created a page that returns hello world.
My cURL looks like this (note that the response is the same even with http://)

curl -vvv localhost:80/hello_world.html

and the response is the following

*   Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 80 failed: Connection refused
* Failed to connect to localhost port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 80: Connection refused

now if i run the following cURL on Powershell
curl http://localhost:80/hello_world.html

the response I get is

StatusCode        : 200
StatusDescription : OK
Content           : <h1> Hello World </h1>
RawContent        : HTTP/1.1 200 OK
                    Keep-Alive: timeout=5, max=100
                    Connection: Keep-Alive
                    Accept-Ranges: bytes
                    Content-Length: 22
                    Content-Type: text/html
                    Date: Mon, 24 Feb 2020 09:52:20 GMT
                    ETag: "16-59f4f46118cab...
Forms             : {}
Headers           : {[Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Accept-Ranges, bytes], [Content-Length, 22]...}    Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 22

which returns the actual hello world and not the error message i get on WSL

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

EDIT:

the server runs in windows 10 machine and the curl also in the same windows 10 machine. Also, the windows are on Insider version 19569.100

UPDATE:
I checked out this Microsoft article that @Anaksunaman shared in the comments and followed the step that says to cat /etc/resolv.conf then from there I got my IP and tried the curl with my IP from resolv.conf and it worked but still with localhost doesn't seem to work also in the article it says

Depending on which version of Windows you're using, you might need to retrieve the IP address of the virtual machine. If your build is 18945 or higher, you can use localhost just like normal.

My build is higher than 18945 as I'm a windows insider user. Is there any way that I can map my IP (EG 111.222.333.444) to localhost? so when I do a CURL to localhost the curl actually uses the IP 111.222.333.444?

Best Answer

I suspect you're actually running WSL2, which handles networking almost entirely differently from how WSL handled it. See this issue on git for a very long breakdown of what's happening and why.

The short answer: it's not going to be easy to get localhost running the way you'd like on WSL2, and I've personally downgraded to WSL (the first version) for the moment until they finally offer a method to do that without both having to map all of your requests to an arbitrary assigned-on-startup IP and opening your Windows firewall rules up to allow public inbound connections on your service port.

If you do want to downgrade and magically have localhost interactions back, just open a Powershell console as admin and enter the following (assuming running an Ubuntu release):

wsl --set-version Ubuntu 1

You'll lose any WSL2 features but regain your sanity. Note that there may be one or two migration tasks to perform on the Ubuntu instance itself.

Related Question