具有自定义域的 WSL 主机文件

Rob*_*ari 5 hosts windows-subsystem-for-linux wsl-2

我创建了一个基本的 Express 应用程序

const express = require('express');

const app = express();

app.get("*", (req, res) => {;
  res.contentType('html');
  res.send("HELLO FROM WSL");
});
const port = 80
app.listen(port);
Run Code Online (Sandbox Code Playgroud)

然后我添加以下条目c:\windows\system32\drivers\etc\hosts

127.0.0.1        custom.local
Run Code Online (Sandbox Code Playgroud)

然后我关闭 wslwsl --shutdown并重新打开以启动我的 Express 应用程序。

如果我从 WSL ( 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       LAPTOP-ZECKA.localdomain        LAPTOP-ZECKA
127.0.0.1       custom.local
Run Code Online (Sandbox Code Playgroud)

然后我去http://custom.local窗户里的槽。但它不显示我的快速应用程序。(如果我在 Windows 上运行express而不是wsl,它工作得很好)。

我的主机文件出了什么问题?

Rob*_*ari 7

最后我在github上找到了解决方案:https://github.com/microsoft/WSL/issues/5728#issuecomment-917295590

相反,像这样声明域

127.0.0.1        custom.local
Run Code Online (Sandbox Code Playgroud)

我这样做:

127.0.0.1   custom.local
::1         custom.local localhost
Run Code Online (Sandbox Code Playgroud)