Search code examples
djangonginxgunicornazure-virtual-machine

How to resolve a 502 Bad Gateway when deploying Django App on Azure with Gunicorn and Nginx


I've been trying to deploy a Django app on Azure for about a week. My instructor followed the exact same setup I used and he was able deploy the same app to Azure.

I've followed several tutorials, this one from Digital Ocean in particular, and I always get the same result. When I go the the IP address I get a 502 Bad Gateway. The only change I make from the tutorial is opening up ports 8000 and 80. I do that through Azure's Networking settings.

I've tried all of the troubleshooting recommendations at the end of that tutorial. I've restarted Gunicorn, Nginx, and even the VM.

On Azure I have a Standard B1s VM running Ubuntu 22.04. I do not have a custom domain, just the static IP assigned by Azure.

I can deploy the app on the development server just fine. If I bind 0.0.0.0:8000 to gunicorn that also will display the site (minus styling).

Gunicorn starts and runs. When I check the status after starting the service it shows the active green dot. Immediately after trying to access the site via the IP address, if I check Gunicorn status again it reads: gunicorn.socket: Failed with result 'service-start-limit-hit'

This is what I get running 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'.

Solution

  • Answering my own question

    The problem was my environment variables were stored inside the project's virtual environment. Gunicorn was not able to access the 'SECRET_KEY' from there. I changed my environment variables to be stored in a .env file and used django-environ package to import the variables into settings.py. Everything works as it should now.

    My project directory looks like this:

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

    If someone had this same issue, they could also export environment variables from .bashrc. I think keeping project variables within the project directory is cleaner. Make sure to add the file with environment variables to .gitignore.