简体   繁体   中英

How to access service running in WSL2 from Windows host using 127.0.0.1?

I have an Express API server up and running on WSL2 on port 8080. I am trying to access the endpoints using 127.0.0.1 from my host Windows machine where I run Postman and my browsers, but I cannot access the API. Changing 127.0.0.1 to localhost works but I was wondering why doesn't 127.0.0.1 work. Perhaps Window's host files are conflicting with this route/forward?

Here is the Windows host file:

#
127.0.0.1 localhost
::1 localhost
# Added by Docker Desktop
192.168.0.14 host.docker.internal
192.168.0.14 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

Here is the WSL2 host file:

# 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       HOST.localdomain   HOST
<feff>
127.0.0.1       localhost
::1     localhost
192.168.0.14    host.docker.internal
192.168.0.14    gateway.docker.internal
127.0.0.1       kubernetes.docker.internal

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

All I want is to hit 127.0.0.1 from Windows and my API running in WSL2 to respond. Is this possible? Thank you for the help!

I needed these things to this to work with WSL2

  1. Upgrade to the latest version of windows
  2. wsl --shutdown in powershell
  3. Settings > Network & Internet > Network Reset as described here
  4. Configure Windows "Advanced" Firewall to open port to all networks (WSL is public)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM