获取 err_connection_refused 从 Windows 访问在 wsl2 上运行的 django,但可以从 Windows 终端卷曲 [英] Get err_connection_refused accessing django running on wsl2 from Windows but can curl from Windows terminal
问题描述
当我尝试从 Windows 访问在 wsl2 (http://localhost:8000) 上运行的 django 时,我得到了 err_connection_refused
,但是当我使用 curl http://localhost:8000
来自Windows终端bash的代码>,它工作正常.我尝试为端口 8000 添加新的防火墙入站规则,但仍然无法正常工作.还有什么需要我照顾的吗?
I got the err_connection_refused
when trying to accessing django running on wsl2 (http://localhost:8000) from Windows but when I use curl http://localhost:8000
from Windows terminal bash, it's working fine. I have tried to add a new firewall inbound rule for port 8000 but it's still not working. Is there anything else I need to take care of.
非常感谢
推荐答案
好像是转发问题.WSL2 的接口是 NAT 的,而 WSL1 是默认桥接的.WSL 似乎做了一些自动转发".端口,但仅在本地主机上.然而,有时这种自动转发机制似乎崩溃".罪魁祸首似乎是休眠或 Windows 快速启动(两者都是密切相关的功能).
Seems like a forwarding problem. WSL2's interface is NAT'd, whereas WSL1 was bridged by default. WSL seems to do some "auto-forwarding" of ports, but only on localhost. However, sometimes this auto-forwarding mechanism seems to "break down". The main culprit seems to be hibernation or Windows Fast Startup (which are both closely-related features).
如果您执行
wsl --shutdown
然后重新启动 WSL2 会话,问题是否会解决?如果是这样,请尝试禁用 Windows 的快速启动.由于我的系统上存在不同的(非 WSL 问题),我已经禁用了快速启动,因此这可能与我 无法重现的原因有关.
Does the problem resolve if you do a
wsl --shutdown
and then restart the WSL2 session? If so, try disabling Windows' Fast Startup. I already had Fast Startup disabled due to a different (non-WSL issue) on my system, so that could be related to why I am not able to reproduce.
同样,你是休眠而不是关机吗?在这种情况下,wsl --shutdown
也可以解决.
Along the same lines, do you Hibernate instead of powering off? In that case, a wsl --shutdown
might resolve as well.
对于未来的读者,请注意,以上两点似乎解决了大多数在评论中投票和回复的人的问题.但是,如果这对您不起作用,以下是我最初的其他建议":
有关其他想法,请参阅此 github 问题.有一些关于可能需要的服务的建议.(附带问题 - 您运行的是 Windows 家庭版还是专业版?)
For some additional ideas, see this github issue. There are some suggestions on services that might be needed. (Side question - Are you running Windows Home or Professional?)
您的 Windows 主机文件(例如 c:windowssystem32driversetchosts
)是否有可能将 localhost 指向 127.0.0.1 以外的 IP?如果我尝试通过本地 Windows IP 地址而不是 127.0.0.1 或 localhost 进行访问,我也会收到 ERR_CONNECTION_REFUSED.
Is there any chance that your Windows hosts file (e.g. c:windowssystem32driversetchosts
) points localhost to an IP other than 127.0.0.1? If I attempt to access via my local Windows IP address, rather than 127.0.0.1 or localhost, I get an ERR_CONNECTION_REFUSED as well.
既然您正在查看防火墙规则,也许可以查看转发规则,而不仅仅是入站允许?
Since you were looking at the firewall rules, maybe look at a forwarding rule instead of just an inbound allow?
如果所有其他方法都失败,请尝试导出/备份 WSL2 会话(请参阅 wsl --export
),然后将其作为新的 WSL1 会话导入.看看它是否在那里工作.
If all else fails, try exporting/backing up the WSL2 session (see wsl --export
), then import it in as a new WSL1 session. See if it works there.
在我的 WSL2/Ubuntu 20.04 系统上,我尝试通过以下步骤重现(但还不能):
On my WSL2/Ubuntu 20.04 system, I attempted to reproduce (but haven't been able to yet) with the following steps:
mkdir -p ~/src/dj-test
cd ~/src/dj-test
python3 -m venv dj
source dj/bin/activate
pip install Django
django-admin startproject config .
python manage.py runserver
(虽然我使用了 activate.fish
,因为我正在运行 fish shell)
(although I used activate.fish
since I'm running the fish shell)
从 Windows 中的 Vivaldi 网络浏览器访问 localhost:8000
,返回安装成功!恭喜!……"
From Vivaldi web browser in Windows, accessed localhost:8000
, which returned "The install worked successfully! Congratulations! ..."
curl
也能正常工作.
这篇关于获取 err_connection_refused 从 Windows 访问在 wsl2 上运行的 django,但可以从 Windows 终端卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!