【问题标题】:How to resolve a 502 Bad Gateway when deploying Django App on Azure with Gunicorn and Nginx使用 Gunicorn 和 Nginx 在 Azure 上部署 Django 应用程序时如何解决 502 Bad Gateway
【发布时间】:2022-12-18 06:00:48
【问题描述】:

我一直在尝试在 Azure 上部署一个 Django 应用程序大约一个星期。我的讲师遵循了我使用的完全相同的设置,他能够将相同的应用程序部署到 Azure。

我遵循了几个教程,特别是this one from Digital Ocean,我总是得到相同的结果。当我访问 IP 地址时,我得到一个 502 Bad Gateway。我对本教程所做的唯一更改是打开端口 8000 和 80。我通过 Azure 的网络设置来做到这一点。

我已经尝试了该教程末尾的所有故障排除建议。我重新启动了 Gunicorn、Nginx,甚至是虚拟机。

在 Azure 上,我有一个运行 Ubuntu 22.04 的标准 B1s VM。我没有自定义域,只有 Azure 分配的静态 IP。

我可以很好地在开发服务器上部署该应用程序。如果我将 0.0.0.0:8000 绑定到 gunicorn,它也会显示该站点(减去样式)。

Gunicorn 启动并运行。当我在启动服务后检查状态时,它会显示活动的绿点。在尝试通过 IP 地址访问站点后,如果我再次检查 Gunicorn 状态,它会立即显示:gunicorn.socket: Failed with result 'service-start-limit-hit'

这就是我运行的sudo journalctl -fu gunicorn

Dec 09 12:12:01 web-ubuntu22-vm1 gunicorn[33891]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
Dec 09 12:12:01 web-ubuntu22-vm1 gunicorn[33891]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
Dec 09 12:12:01 web-ubuntu22-vm1 systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
Dec 09 12:12:01 web-ubuntu22-vm1 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Dec 09 12:12:01 web-ubuntu22-vm1 systemd[1]: gunicorn.service: Start request repeated too quickly.
Dec 09 12:12:01 web-ubuntu22-vm1 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Dec 09 12:12:01 web-ubuntu22-vm1 systemd[1]: Failed to start gunicorn daemon.
Dec  9 12:12:01 web-ubuntu22-vm1 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.

【问题讨论】:

    标签: django nginx gunicorn azure-virtual-machine


    【解决方案1】:

    回答我自己的问题

    问题是我的环境变量存储在项目的虚拟环境中。 Gunicorn 无法从那里访问“SECRET_KEY”。我将环境变量更改为存储在.env 文件中,并使用django-environ 包将变量导入settings.py。现在一切正常。

    我的项目目录如下所示:

    django-project
       ├── django-app
       ├── .env
       ├── .gitignore
       ├── django-project
       ├── manage.py
       ├── README.md
       ├── requirements.txt
       ├── static
       └── venv
    

    如果有人有同样的问题,他们也可以从.bashrc导出环境变量。我认为将项目变量保存在项目目录中更清晰。确保将带有环境变量的文件添加到.gitignore

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2020-12-14
      • 2018-08-03
      • 2020-06-12
      • 2017-04-13
      • 2016-04-20
      • 2014-04-30
      • 2017-01-04
      • 2018-08-22
      相关资源
      最近更新 更多