You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL容器无法通过IP地址连接,PHP内置服务器外部访问失败求助

WSL容器无法通过IP地址连接,PHP内置服务器外部访问失败求助

我最近碰到个棘手的问题,在WSL(Ubuntu 20.04.1)里启动PHP内置服务器php -S localhost:80后,WSL内部能正常访问,但在Windows 11主机上死活连不上,想请大家帮忙支支招。

先说说具体情况:

  • 用ifconfig查到WSL的eth0网卡IP是172.18.240.175
  • 在WSL内部,不管是用127.0.0.1:80还是这个eth0 IP都能正常连接,比如执行php -r "fsockopen('127.0.0.1', 80);"完全没问题
  • 但在Windows主机上,用这个eth0 IP尝试连接时直接报错,执行php -r "fsockopen('172.18.240.175', 80);"会得到:

Warning: fsockopen(): unable to connect to 172.18.240.175:80 (No connection could be made because the target machine actively refused it.
) in php shell code on line 1

我已经试过这些操作,但都没解决问题:

  1. 添加iptables规则允许80端口访问:
    sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
    
    执行sudo iptables -nL -v | grep 80能看到这条规则已经生效,而且每次我发起请求,pkts和bytes的数值都会增加,说明WSL确实收到了请求,但就是被拒绝了:
    5   260 ACCEPT     tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    
  2. 检查UFW防火墙状态,显示Status: inactive,但还是尝试执行了sudo ufw allow 80/tcp,结果依旧没用。

另外我本来想用Wireshark抓包排查,但不知道怎么抓取WSL的数据包...

有没有大佬遇到过类似的问题,能给点排查思路或者解决方案吗?

备注:内容来源于stack exchange,提问作者neubert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 10:49:29