简体   繁体   English

来自 windows 的 http://localhost:4200 正在与来自 WSL2 的 ng serve 一起工作,这是怎么回事?

[英]http://localhost:4200 from windows is working with ng serve from WSL2, how come?

When I run "ng serve" from WSL2 (Ubuntu 20.04), I am able to access the angular application from Windows 10 Host by browsing to http://localhost:4200.当我从 WSL2 (Ubuntu 20.04) 运行“ng serve”时,我可以通过浏览到 http://localhost:4200 从 Windows 10 主机访问 angular 应用程序。

Can someone explain how this is possible?有人可以解释这怎么可能吗? I understand that WSL2 is working as a virtual machine with its own ip address.我知道 WSL2 作为具有自己的 IP 地址的虚拟机工作。 How can Windows 10 host access the angular app on WSL2 using "http://localhost:4200" ? Windows 10 主机如何使用“http://localhost:4200”访问 WSL2 上的 angular 应用程序?

On a different note, when I run tomcat from WSL2, I am NOT able to access the tomcat application from Windows using "http://localhost:8080".另一方面,当我从 WSL2 运行 tomcat 时,我无法使用“http://localhost:8080”从 Windows 访问 tomcat 应用程序。

Thanks in advance Paul提前致谢保罗

The assumption that WSL2 is working as a virtual machine with its own ip address depends on the Windows 10 version. WSL2 is working as a virtual machine with its own ip address的假设取决于 Windows 10 版本。

Also read this article .另请阅读这篇文章 It explains why and how.它解释了原因和方式。

Accessing Linux networking apps from Windows (localhost)从 Windows (localhost) 访问 Linux 网络应用程序

If you are building a networking app (for example an app running on a NodeJS or SQL server) in your Linux distribution, you can access it from a Windows app (like your Edge or Chrome internet browser) using localhost (just like you normally would).如果您正在 Linux 发行版中构建网络应用程序(例如在 NodeJS 或 SQL 服务器上运行的应用程序),您可以使用 localhost 从 Windows 应用程序(例如您的 Edge 或 Chrome 互联网浏览器)访问它(就像您通常使用的那样) )。

However, if you are running an older version of Windows (Build 18945 or less), you will need to get the IP address of the Linux host VM (or update to the latest Windows version).但是,如果您运行的是较旧版本的 Windows(Build 18945 或更低版本),则需要获取 Linux 主机 VM 的 IP 地址(或更新到最新的 Windows 版本)。

I had a similar problem: The default React server worked fine, but the Tomcat server wouldn't even if they were running on the same port.我有一个类似的问题:默认的 React 服务器工作正常,但 Tomcat 服务器即使它们运行在同一个端口上也不会。 Dudi Boy's answer suggests using a specific IP on older WSL versions, but that didn't help me much because I'm on a newer version. Dudi Boy 的回答建议在较旧的 WSL 版本上使用特定的 IP,但这对我没有多大帮助,因为我使用的是较新的版本。 However, his link contained another useful piece of advice但是,他的链接包含另一个有用的建议

Connecting via remote IP addresses通过远程 IP 地址连接

When using remote IP addresses to connect to your applications, they will be treated as connections from the Local Area Network (LAN).当使用远程 IP 地址连接到您的应用程序时,它们将被视为来自局域网 (LAN) 的连接。 This means that you will need to make sure your application can accept LAN connections.这意味着您需要确保您的应用程序可以接受 LAN 连接。

For example, you may need to bind your application to 0.0.0.0 instead of 127.0.0.1 .例如,您可能需要将应用程序绑定到0.0.0.0而不是127.0.0.1 In the example of a Python app using Flask, this can be done with the command: app.run(host='0.0.0.0') .在使用 Flask 的 Python 应用程序示例中,这可以通过以下命令完成: app.run(host='0.0.0.0') Please keep security in mind when making these changes as this will allow connections from your LAN.进行这些更改时请牢记安全性,因为这将允许从您的 LAN 连接。

Sure enough, when I had my server run on 0.0.0.0 instead of 127.0.0.1 , it worked!果然,当我让我的服务器在0.0.0.0而不是127.0.0.1上运行时,它工作了!

暂无
暂无

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

相关问题 从 http://localhost:4200 的末尾删除# - remove # from the end of http://localhost:4200 从 http 转换为 https(http://localhost:4200 到 https://localhost:4200<or secured port> ) - Convert from http to https (http://localhost:4200 to https://localhost:4200<or secured port>) POST http://localhost:4200/ 404 未找到(从 Angular 到 Nodejs 的请求) - POST http://localhost:4200/ 404 Not found (Request from Angular to Nodejs) 显示来自Firebase的图像时获取http:// localhost:4200 / undefined - GET http://localhost:4200/undefined while displaying image from firebase ng serve上的Angular 6将localhost:4200更改为domain:name - Angular 6 on ng serve change localhost:4200 to domain:name CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:8082/kanchiwork/”处的 XMLHttpRequest - Access to XMLHttpRequest at 'http://localhost:8082/kanchiwork/' from origin 'http://localhost:4200' has been blocked by CORS policy CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:8080”处的 XMLHttpRequest - Access to XMLHttpRequest at 'http://localhost:8080 from origin 'http://localhost:4200' has been blocked by CORS policy 从&#39;http:// localhost:4200&#39;访问&#39;http:// localhost:1111 / ...&#39;的XMLHttpRequest已被CORS策略阻止: - Access to XMLHttpRequest at 'http://localhost:1111/…' from origin 'http://localhost:4200' has been blocked by CORS policy: 从源“http://localhost:4200”获取“http://localhost:8080/BeginSignup”的访问已被 CORS 策略阻止 - Access to fetch at 'http://localhost:8080/BeginSignup' from origin 'http://localhost:4200' has been blocked by CORS policy ng服务不在Windows中工作 - ng serve not working in windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM