代码之家  ›  专栏  ›  技术社区  ›  markvgti

从Windows主机连接到WSL2上的服务器不起作用

  •  0
  • markvgti  · 技术社区  · 3 年前

    更新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)访问(见屏幕截图)。

    Can't reach WSL2 server via browser either

    在绝望中,我尝试了以下方法,但都没有奏效:

    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) .

    0 回复  |  直到 3 年前
        1
  •  4
  •   NotTheDr01ds    3 年前

    我不熟悉Quarkus,但我的“去 answer “对于在WSL中不起作用的localhost转发,请尝试 wsl --shutdown 如果有效,请禁用Windows快速启动。

    但在这种情况下,您也无法在WSL2实例的eth0地址上访问它,因此 可以 如果Quarkus样板配置只是绑定到 localhost 127.0.0.1 。你需要它才能绑定到 0.0.0.0 以便两者都能工作。

    为将来看到此答案的其他人更新 :Quarkus自动绑定到 127.0.0.1 本地服务器 事实证明这是本案的问题所在。根据@markvgti在评论中的研究,这一设置是添加 quarkus.http.host=0.0.0.0 application.properties .

        2
  •  0
  •   Joker    3 年前

    如NotTheDr01ds答案更新中所建议的:

    添加 quarkus.http.host=0.0.0.0 application.properties 为我工作