从 Windows 获取 err_connection_refused 访问在 wsl2 上运行的 django,但可以从 Windows 终端 curl [英] Get err_connection_refused accessing django running on wsl2 from Windows but can curl from Windows terminal

查看:34
本文介绍了从 Windows 获取 err_connection_refused 访问在 wsl2 上运行的 django,但可以从 Windows 终端 curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从 Windows 访问在 wsl2 (http://localhost:8000) 上运行的 django 时,我得到了 err_connection_refused 但是当我使用 curl http://localhost:8000code> 来自 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 因为我运行的是鱼壳)

(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 在 Powershell Core 下也能正常工作.

curl under Powershell Core worked as well.

这篇关于从 Windows 获取 err_connection_refused 访问在 wsl2 上运行的 django,但可以从 Windows 终端 curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆