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

0

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?

Theodosis

Posted 2020-02-24T09:55:52.840

Reputation: 101

To clarify, is the server running under Windows and the plain curl command under WSL? – Anaksunaman – 2020-02-24T12:15:24.763

They are all on the same windows pc both application and wsl – Theodosis – 2020-02-25T08:36:00.323

At a guess, it's possible that this issue is due to WSL. It's a little old but see this Microsoft article. It's also probably worth mentioning that curl has up-to-date native Windows builds you can test with that don't rely on WSL. Otherwise, you may want to check your server software and firewalls to ensure none of those is blocking curl's access to your server.

– Anaksunaman – 2020-02-25T09:08:18.840

That said, if you still run into problems, it might be good to [edit] your question and detail the exact steps and software (including relevant config files) you are using, which could help resolve any non-obvious issues. – Anaksunaman – 2020-02-25T09:12:06.257

Okay, thank you I'm going to check it. Also, note that the server is a simple apache2 and right now I have disabled some parts of the firewall in order to ping my public IP and this works. (Eg curl -vvv localhost:80/hello_world.html doesn't work but curl -vvv 111.222.333.4444:80/hello_world.html works.) (the IPs are fake only for this example) – Theodosis – 2020-02-25T09:13:32.933

You're welcome. That's helpful information. Feel free to update your question with any test results (as desired). – Anaksunaman – 2020-02-25T09:21:28.203

I have updated my question as the Microsoft article didn't help to solve my issue still thanks for your help – Theodosis – 2020-02-26T10:16:32.813

No answers