获取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时,但当我使用 curl http://localhost:8000 时,得到了
err_connection_refused
Windows终端bash中的code>,它工作正常.我试图为端口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 Home还是Professional?)
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:\ windows \ system32 \ drivers \ etc \ hosts
)是否有可能将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:\windows\system32\drivers\etc\hosts
) 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
)
(although I used activate.fish
since I'm running the fish shell)
在Windows中的Vivaldi Web浏览器中,访问 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屋!