【问题标题】:How to access service running in WSL2 from Windows host using 127.0.0.1?如何使用 127.0.0.1 从 Windows 主机访问在 WSL2 中运行的服务?
【发布时间】:2021-01-14 14:47:03
【问题描述】:

我有一个 Express API 服务器启动并在 WSL2 上的 8080 端口上运行。我正在尝试使用 127.0.0.1 从运行 Postman 和浏览器的 Windows 主机访问端点,但我无法访问 API。将127.0.0.1 更改为localhost 有效,但我想知道为什么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

这是 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

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

【问题讨论】:

    标签: windows api localhost windows-subsystem-for-linux wsl-2


    【解决方案1】:

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

    1. Upgrade to the latest version of windows
    2. wsl --shutdown 在 powershell 中
    3. 设置>网络和Internet>网络重置为described here
    4. 配置 Windows“高级”防火墙以打开所有网络的端口(WSL 是公共的)

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2021-04-13
      • 2021-01-02
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2018-04-14
      相关资源
      最近更新 更多