Django Project on Azure, 502 error after deployment
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.
Can you please provide the logs from Azure? Do they indicate any kind of error? Do you have access to a console in the Azure Web App? What does it say if you try ping the service from the webapp itself?
keep getting a 502 Bad Gateway error.
You're getting a bad gateway error because gunicorn
isn't installed correctly.
- Add
gunicorn
to yourrequirements.txt
file and make sure it’s installed. - Make sure your
wsgi.py
file correctly sets theDJANGO_SETTINGS_MODULE
wsgi .py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_wsgi_application()
I've created a sample Django application and successfully deployed it to Azure App Service without any issues.
My settings .py:
In settings.py
, allow all hosts
ALLOWED_HOSTS = ['*']
from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = '<secret-key>'
DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'myproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'myproject.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
requirements.txt:
asgiref==3.8.1
Django==5.1.6
gunicorn==23.0.0
packaging==24.2
sqlparse==0.5.3
tzdata==2025.1
Run these commands before deployment:
python manage.py collecstatic
python manage.py migrate
If necessary, manually set the startup command
gunicorn --bind=0.0.0.0:8000 myproject.wsgi
The application has been successfully deployed to Azure App Service via GitHub Actions.
Production output: