gpt4 book ai didi

python - Azure VM Gunicorn 上 Ubuntu 18.04 中的 Django 无法连接到 ('0.0.0.0' , 80)

转载 作者:行者123 更新时间:2023-12-03 02:53:24 25 4
gpt4 key购买 nike

我正在尝试使用 Ubuntu 18.04 将 Django 应用程序部署到 Azure 虚拟机。

  1. 我已设置虚拟机并通过 SSH 连接到它。
  2. 然后运行更新和升级命令
  3. 设置 Python 和虚拟环境
  4. 上传我的代码并激活环境
  5. 使用 sudo ufw allowed 8000 允许端口 8000 进行测试
  6. 安装所有要求后,当我运行命令时:

    python manage.py runserver 0.0.0.0:8000

应用程序运行,但是当我打开 URL 时::8000/

它不会在控制台中返回任何内容,也不会返回任何错误

Update: It's just fixed by manually adding the port 8000 in azure portal under Inbound port rules. But: when I try to run it via gunicorn as:

gunicorn --pythonpath PROJECT PROJECT.wsgi:application --log-file - --bind 0.0.0.0:80

它返回另一个错误,如下所示:

[30007] [ERROR] Can't connect to ('0.0.0.0', 80)

这里可能出了什么问题?

最佳答案

解决通过python manage.py runserver 0.0.0.0:8000运行应用程序无法访问的问题,有两个原因导致该问题。

  1. Azure VM NSG 的入站端口规则不允许对端口 8000 的入站请求。可在 Azure 门户上的 NSG 中为端口 8000 添加新的端口规则来修复此问题,如下图.

图1.图中添加这条规则,允许8000端口入站请求

enter image description here

图2.添加入站安全规则对话框

enter image description here

  • 编辑 settings.py 文件,将允许的主机或 IP 添加到 ALLOWED_HOSTS 数组中,如下所示。

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = ['<your vm ip or DNS name>', 'localhost', '127.0.0.1']
  • 然后运行python manage.py runserver 0.0.0.0:8000,可以在浏览器中看到Django默认索引页面,没有任何错误,如下图。

    注意:gunicorn 服务器监听端口 80,这是默认允许的入站端口规则。

    enter image description here

    关于python - Azure VM Gunicorn 上 Ubuntu 18.04 中的 Django 无法连接到 ('0.0.0.0' , 80),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663323/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com