简体   繁体   English

如何为在 WSL 2 中运行的服务器(例如 node.js)设置自定义主机名和端口

[英]How to set up custom hostnames and ports for servers (eg node.js) running in WSL 2

(I've provided a simple working solution in response) (作为回应,我提供了一个简单的工作解决方案)

I recently moved from macOS to WSL 2. I have two node servers running within WSL 2 (Ubuntu distro).我最近从 macOS 迁移到 WSL 2。我在 WSL 2(Ubuntu 发行版)中运行了两个节点服务器。 Each must be accessible through a custom hostname for development vs production purposes.每个都必须通过自定义主机名进行访问,以用于开发和生产目的。 I've had difficulty accessing the node servers via custom hostnames (ie set in some../etc/hosts file) especially given WSL 2's dynamic IP that changes per WSL/pc 'boot'.我很难通过自定义主机名(即在某些../etc/hosts 文件中设置)访问节点服务器,尤其是考虑到 WSL 2 的动态 IP 会随着 WSL/pc 的“启动”而改变。 How does one go about setting custom hostnames in WSL 2? go 如何在 WSL 2 中设置自定义主机名?

Scenario:设想:

Each node.js app server (again running within WSL 2) must be accessed from the browser with the following urls/custom hostnames:每个 node.js 应用服务器(再次在 WSL 2 中运行)必须使用以下 url/自定义主机名从浏览器访问:

After searching around I have found the following relatively simple process works.在四处搜索后,我发现以下相对简单的过程有效。 I thought I'd share and save some time and headache for those new to WSL 2. Note, although I'm using node as the server stack, this process should more or less be the same for other app/web server stacks.我想我会为那些刚接触 WSL 2 的人分享并节省一些时间和头痛。注意,虽然我使用节点作为服务器堆栈,但对于其他应用程序/Web 服务器堆栈,这个过程应该或多或少是相同的。

Note the following SE post is the basis of the solution.请注意, 以下SE 帖子是解决方案的基础。 It's also worthwhile to examine MSFT's reference on WSL vs WSL 2 .检查 MSFT 关于WSL 与 WSL 2的参考也是值得的。 Also note, I haven't provided deep rationale on why these steps are required, why we might need custom hostnames, ipv6 options in../etc/hosts, the meaning of 127.0.0.1, loopback addresses, WSL 2 and distro management, etc. These are subjects beyond the scope of this post.另请注意,我没有详细说明为什么需要这些步骤、为什么我们可能需要自定义主机名、../etc/hosts 中的 ipv6 选项、127.0.0.1 的含义、环回地址、WSL 2 和发行版管理,等等。这些是本文 scope 之外的主题。

Simple scenario:简单场景:

  • nodeApp1: node application server with custom hostname: 'www.app1.com' on port 3010 (or whatever) nodeApp1:具有自定义主机名的节点应用程序服务器:端口 3010 上的“www.app1.com”(或其他)
  • nodeApp2: node application serverwith custom hostname: 'www.app2.com' on port 3020 (or whatever) nodeApp2:具有自定义主机名的节点应用程序服务器:端口 3020 上的“www.app2.com”(或其他)

Each node.js app server (again running within wsl 2) can be accessed from the browser with the following urls:每个 node.js 应用服务器(再次在 wsl 2 中运行)都可以通过以下 url 从浏览器访问:

Two key items:两个关键项目:

  • The correct etc/hosts files to be modified is on the Windows side (not WSL distro) at: C:\Windows\System32\drivers\etc\hosts (yes in Windows folders).要修改的正确 etc/hosts 文件位于 Windows 端(不是 WSL 发行版),位于: C:\Windows\System32\drivers\etc\hosts (在ZAEA23489CE3AAA9B63406EBB28E0CD文件夹中是) This is a 'hot' update so no need for WSL 2 reboot.这是一个“热”更新,因此无需重新启动 WSL 2。 The content for this scenario is:这个场景的内容是:
 127.0.0.1 localhost 127.0.0.1 www.app1.com 127.0.0.1 www.app2.com 255.255.255.255 broadcasthost::1 localhost www.app1.com www.app2.com
  • Please add C:\Users\"you"\.wslconfig with the following content (yes in Windows folders):请添加C:\Users\"you"\.wslconfig与以下内容(是在Windows文件夹中):
 [wsl2] localhostForwarding=true
  • Note: there's a reference to this in WSL 2 Ubuntu distro's /etc/hosts.注意:在 WSL 2 Ubuntu 发行版的 /etc/hosts 中有对此的引用。
  • Also note, this requires WSL shutdown and reboot.另请注意,这需要 WSL 关闭并重新启动。 Shutting down your terminal is insufficient.关闭终端是不够的。 Also total machine boot is not required.也不需要整个机器引导。 Simply run:只需运行:
wsl --shutdown (in Powershell) or 
wsl.exe --shutdown (within Ubuntu)

Then restart the Windows Terminal app (or any WSL terminal) to access the updated WSL 2 environment.然后重新启动 Windows 终端应用程序(或任何 WSL 终端)以访问更新后的 WSL 2 环境。 The apps with custom urls/hostnames will now work in the browser permanently and WSL 2's dynamic IP is circumvented.具有自定义 url/主机名的应用程序现在将在浏览器中永久运行,并且绕过 WSL 2 的动态 IP。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM