can't access 127.0.0.1:80 outside of WSL2 / Ubuntu 20.04
Solution 1:
That's because each host has a localhost 127.0.0.1. The windows host thinks it's going to itself. The easiest solution is for you to IP with a real IP. example 192.168.x.x
I found the following blurb
In the early stages of WSL 2, we can’t use localhost. We need to use an IP since Linux is inside a VM.
To access your application currently, we will use an IP.
Solution 2:
Problem:
Windows 10 thinks localhost is ::1
.
PS C:\Users\michael> ping localhost
Pinging DESKTOP[::1] with 32 bytes of data:
Reply from ::1: time<1ms
Ubuntu thinks localhost is 127.0.0.1
$ ~ > cat /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 DESKTOP.localdomain DESKTOP
Current Solution: 7/27/2021
WSL is changing relatively quickly.
My current solution w/ WSL2 is
- "Start->Network Reset (system settings)->Reset
- Casually reboot windows like it's 1998.
- Get on with my life.
Solution 1:
- Read this documentation
- Notice the obscure note in wsl settings about true except for some builds.
- Add a file with these contents to
C:\Users\Me\.wslconfig
:
[wsl2]
localhostForwarding=true
This is what worked for me.
Solution 2:
This didn't work so well for me, but might for you. Stop automatic generation as instructed in the comments in /etc/hosts
, and change localhost to ::1
so they refer to the same adapter. For compatibility, 0.0.0.0
binds to all interfaces (127.0.0.1 and ::1) so it works.
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 ip4-localhost
::1 localhost
...
Will this mess things up? Probably. If it does, you'll have to remember to fiddle this to get that to work.
Solution 3:
This is what worked for me, as with localhost/127.0.0.1 I had no luck:
- get WSL2 linux IP using ifconfig:
aristos32@DESKTOP-V493N00:~$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.21.115.144 netmask 255.255.240.0 broadcast 172.21.127.255 inet6 fe80::215:5dff:fea0:1a98 prefixlen 64 scopeid 0x20<link>
- use this IP in the browser instead of localhost
http://172.21.115.144/