如何使用 127.0.0.1 从 Windows 主机访问 WSL2 中运行的服务?

bom*_*azo 5 windows api localhost windows-subsystem-for-linux wsl-2

我在 WSL2 的端口 8080 上启动并运行了 Express API 服务器。我尝试127.0.0.1从运行 Postman 和浏览器的 Windows 主机访问端点,但无法访问 API。更改127.0.0.1localhost有效,但我想知道为什么不起作用127.0.0.1。也许 Window 的主机文件与此路由/转发冲突?

这是 Windows 主机文件:

#
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
Run Code Online (Sandbox Code Playgroud)

这是 WSL2 主机文件:

# 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
Run Code Online (Sandbox Code Playgroud)

我想要的只是127.0.0.1从 Windows 和 WSL2 中运行的 API 进行响应。这可能吗?感谢您的帮助!

Mic*_*ole 3

我需要这些东西才能与 WSL2 配合使用

  1. 升级到最新版本的Windows
  2. wsl --shutdown在 powershell 中
  3. 设置 > 网络和 Internet > 网络重置,如此处所述
  4. 配置 Windows“高级”防火墙以打开所有网络的端口(WSL 是公共的)