更新1
如果我将代码打包为Docker镜像&运行它,curl命令在WSL2内部工作
和
从Windows命令行,所以这是Quarkus特定的问题。
起初的
我在我的Windows家庭笔记本电脑上的WSL2上安装了Ubuntu 20.04。
遵循
Quarkus Getting Started
示例(直到步骤5:
运行应用程序
),我的WSL2上运行了代码(服务器)
bash
命令行,&用呼叫
curl
给出了预期的输出:
user@computer:/path$ curl -w '\n' http://localhost:8080/hello
Hello RESTEasy
然而
我无法从主机Windows计算机的命令行访问该服务器(
cmd
):
C:\Users\username>curl -w "\n" http://localhost:8080/hello
curl: (56) Recv failure: Connection was reset
与此不同
WSL 2 Networking
视频,我没有试图从Windows主机外部访问正在运行的服务器。我甚至无法从Windows主机访问WSL2 Ubuntu内部运行的服务器。
尽管如此,我还是尝试了这个稍微修改过的脚本
from an issue on Github
:
#$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$remoteport = bash.exe -c "ip addr | grep -Ee 'inet.*eth0'"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(8080);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL2-Firewall-Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL2-Firewall-Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL2-Firewall-Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
在更改了一些设置后,脚本运行得很好,但我仍然无法访问WSL2上运行的服务器。也无法通过浏览器(Vivaldi)访问(见屏幕截图)。
在绝望中,我尝试了以下方法,但都没有奏效:
C:\Users\username>curl -w "\n" http://192.168.1.6:8080/hello
curl: (56) Recv failure: Connection was reset
C:\Users\username>curl -w "\n" http://172.21.240.1:8080/hello
curl: (56) Recv failure: Connection was reset
C:\Users\username>curl -w "\n" http://172.21.251.69:8080/hello
curl: (7) Failed to connect to 172.21.251.69 port 8080: Connection refused
哪里
-
192.168.1.6
是我的Windows主机的IP地址
-
172.21.240.1
是的IP地址
vEthernet(WSL)
适配器
-
172.21.251.69
是在WSL2中运行的Ubuntu 20.04的IP地址(通过运行
ip addr | grep eth0
). 我可以
ping 172.21.251.69
从…起
cmd
.
我做错了什么?我想
curl -w "\n" http://localhost:8080/hello
将
只是工作
从Windows主机。
Winver报告Windows版本为
版本2004(操作系统内部版本19041.1110)
.