簡體   English   中英

如何使用 127.0.0.1 從 Windows 主機訪問在 WSL2 中運行的服務?

[英]How to access service running in WSL2 from Windows host using 127.0.0.1?

我在 WSL2 端口 8080 上啟動並運行了一個 Express API 服務器。我試圖從運行 Postman 和瀏覽器的主機 Windows 機器上使用127.0.0.1訪問端點,但我無法訪問 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 進行響應。 這可能嗎? 感謝您的幫助!

我需要這些東西才能使用 WSL2

  1. 升級到最新版本的windows
  2. wsl --shutdown中的wsl --shutdown
  3. 設置 > 網絡和 Internet > 網絡重置,如此處所述
  4. 配置 Windows“高級”防火牆以打開所有網絡的端口(WSL 是公共的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM