简体   繁体   English

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

[英]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.我在 WSL2 端口 8080 上启动并运行了一个 Express API 服务器。我试图从运行 Postman 和浏览器的主机 Windows 机器上使用127.0.0.1访问端点,但我无法访问 API。 Changing 127.0.0.1 to localhost works but I was wondering why doesn't 127.0.0.1 work.127.0.0.1更改为localhost有效,但我想知道为什么127.0.0.1不起作用。 Perhaps Window's host files are conflicting with this route/forward?也许 Window 的主机文件与此路由/转发冲突?

Here is the Windows host file:这是 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

Here is the WSL2 host file:这是 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

All I want is to hit 127.0.0.1 from Windows and my API running in WSL2 to respond.我想要的只是从 Windows 命中127.0.0.1并且我在 WSL2 中运行的 API 进行响应。 Is this possible?这可能吗? Thank you for the help!感谢您的帮助!

I needed these things to this to work with WSL2我需要这些东西才能使用 WSL2

  1. Upgrade to the latest version of windows 升级到最新版本的windows
  2. wsl --shutdown in powershell wsl --shutdown中的wsl --shutdown
  3. Settings > Network & Internet > Network Reset as described here设置 > 网络和 Internet > 网络重置,如此处所述
  4. Configure Windows "Advanced" Firewall to open port to all networks (WSL is public)配置 Windows“高级”防火墙以打开所有网络的端口(WSL 是公共的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使wsl2在Windows 10上从停止状态变为运行状态 - How to make wsl2 from stopped to running on Windows 10 无法再从 Windows 资源管理器访问 WSL2 文件或从 WSL2 启动 Windows 程序 - Can no longer access WSL2 files from Windows explorer or launch Windows programs from WSL2 无法从 Windows 主机连接到 WSL2 上的本地服务器 - Unable to connect to local server on WSL2 from Windows host 使用实际 IP 从 WSL2 连接到主机 - Connectivity from WSL2 to host by using actual IP 如何在 Windows 10 上关闭 ssh 后保持 WSL2 运行? - How to keep WSL2 running after closing ssh on Windows 10? 我可以在运行 Windows/WSL2 的远程主机上的容器中使用 VS Code 进行开发吗? - Can I develop with VS Code in containers on a remote host running Windows/WSL2? Windows 主机 Docker + WSL2 - 如何将 Windows 目录挂载到 ZEDC9F0A5A5D57797BF68E373674 容器 - Windows Host Docker + WSL2 - How to mount Windows directory to a Linux container 如何在 Windows WSL2 上使用 sshuttle - How to Use sshuttle on Windows WSL2 在容器内运行时如何访问 flink Web UI(wsl2) - How to access the flink web UI when running inside a container (wsl2) 我无法使用 WSL2 中的 git 在 Windows 上签署来自 IntellIJ 的提交 - I can't sign commits from IntellIJ on windows using git from WSL2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM