无法在 WSL2 / Ubuntu 20.04 之外访问 127.0.0.1:80

neu*_*ert 9 hyper-v ubuntu windows-10 wsl2 ubuntu-20.04

我在 WSL2/Hyper-V/Windows 10 上运行 Ubuntu 20.04。

当我curl http://127.0.0.1在 Ubuntu 里面做的时候,我得到了这个:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.19.3</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我在主机上执行此操作时 - 在 Windows 10 本身(使用 Git Bash)上,我得到以下信息:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
Run Code Online (Sandbox Code Playgroud)

如何在 Ubuntu 之外访问端口 80?我是否需要更新 Windows 中的防火墙规则?

小智 12

这对我有用,就像 localhost/127.0.0.1 我没有运气:

  1. 使用 ifconfig 获取 WSL2 linux IP:
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>
Run Code Online (Sandbox Code Playgroud)
  1. 在浏览器中使用此 IP 而不是 localhost

http://172.21.115.144/

  • 我的问题是每次重新启动计算机时IP都会改变。我需要一个从不(或很少)更改的 IP(或在 Ubuntu WSL 中访问本地主机)。 (2认同)

Mar*_*eck 10

这是因为每个主机都有一个 localhost 127.0.0.1。Windows 主机认为它会自行处理。最简单的解决方案是您使用真实 IP 进行 IP 分配。示例 192.168.xx

\n

我发现了以下简介

\n

在 WSL 2 的早期阶段,我们可以\xe2\x80\x99t 使用 localhost。我们需要使用 IP,因为 Linux 位于虚拟机内。

\n

目前,为了访问您的应用程序,我们将使用 IP。

\n

  • “我发现了以下简介” - 请注明来源 (10认同)
  • @neubert 使用计划任务:https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/ (2认同)

Mic*_*ole 10

问题:

Windows 10 认为 localhost 是::1.

PS C:\Users\michael> ping localhost

Pinging DESKTOP[::1] with 32 bytes of data:
Reply from ::1: time<1ms
Run Code Online (Sandbox Code Playgroud)

Ubuntu 认为 localhost 是 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
Run Code Online (Sandbox Code Playgroud)

当前解决方案:7/27/2021

WSL 的变化相对较快。

我目前使用 WSL2 的解决方案是

  1. “开始->网络重置(系统设置)->重置
  2. 像 1998 年那样随意重启 Windows。
  3. 继续我的生活。

解决方案1:

  1. 阅读本文档
  2. 请注意 wsl 设置中关于 true 的晦涩注释,除了某些构建。
  3. 将包含这些内容的文件添加到C:\Users\Me\.wslconfig
[wsl2]
localhostForwarding=true
Run Code Online (Sandbox Code Playgroud)

这对我有用。

解决方案2:

这对我来说不是很好,但可能对你有用。按照 中的注释中的说明停止自动生成/etc/hosts,并将 localhost 更改为,::1以便它们引用相同的适配器。为了兼容性,0.0.0.0绑定到所有接口(127.0.0.1 和 ::1)所以它可以工作。

# 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

...
Run Code Online (Sandbox Code Playgroud)

这会搞砸吗?大概。如果是这样,您将不得不记住摆弄它以使其工作。

  • OP 应该将此标记为正确的解决方案,“localhostForwarding”配置为我解决了这个问题。现在,我不必每次想要启动开发服务器时都打开 VSCode(出于某种原因,单击 VSCode 终端的 localhost URL 输出可以工作,但如果我手动将其输入到浏览器中,则无法工作)。现在一切都按预期进行,谢谢! (3认同)

Jan*_*ano 5

您可以在 Windows 中使用 localhost 的 IPv6 地址http://[::1]/http://localhost。两者都适用于 Windows 11 和 WSL2。