502 error django deployment

Cornelis Buitelaar 0 Reputation points
2025-02-15T18:54:36.5066667+00:00

I'm deploying a Django app on Azure Web App using Gunicorn and keep getting a 502 Bad Gateway error. Despite binding to the correct port, setting up environment variables for Key Vault, and enabling SCM_DO_BUILD_DURING_DEPLOYMENT for Oryx builds, the site never fully starts. I need to figure out why Gunicorn won't serve the app properly, even though the deployment logs seem fine.

I set SCM_DO_BUILD_DURING_DEPLOYMENT=true, placed my requirements.txt in the project root so Oryx could install dependencies, and updated my startup command to gunicorn --bind=0.0.0.0:$PORT. I also configured environment variables for Key Vault, and tested collecting static files locally vs. on Azure. I expected the Azure Web App to run Gunicorn without errors. Instead, I keep getting a 502 Bad Gateway, and my logs show no direct failure message—just that the service never properly responds.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alekhya Vaddepally 1,670 Reputation points Microsoft External Staff Moderator
    2025-03-06T12:45:05.8233333+00:00

    Hi Cornelis Buitelaar,

    It looks like you've set up your Django app correctly on Azure Web App, but you're running into a 502 Bad Gateway error, which typically means that Gunicorn is either failing to start or crashing after startup.

    Since your deployment logs don’t show a clear failure message, you can also troubleshoot issue step by step

    check that arguments you're passing to Gunicorn are correct

    command: unicorn --bind=0.0.0. PORT is correct but verify that the $PORT environment variable is set correctly in your Azure configuration.

    Also check Django app is structure correctly in Azure App service otherwise it may not be able to find application serve.

    check that static files are collected properly. If you tested collecting static files locally, verify that the same process works in Azure.

    Double-check that all necessary environment variables, including those for Key Vault, are set correctly and accessible by your application.

    Check your environment variables in Azure Portal > Configuration > Application Settings.

    Run print(os.environ) in a Django shell (python manage.py shell) inside SSH to confirm if variables are accessible.

    https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#troubleshooting

    https://learn.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app-flask

    https://learn.microsoft.com/en-us/azure/developer/python/tutorial-deploy-python-web-app-azure-container-apps-02?tabs=azure-cli%2Cconfigure-database-azure-cli#troubleshoot-deployment

    If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.

    Let me know if you have any further Queries.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.