admin管理员组

文章数量:1245093

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.

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.

Share Improve this question asked Feb 15 at 16:05 OllieOllie 136 bronze badges 5
  • Try use this Startup command gunicorn --bind=0.0.0.0 --timeout 600 <yourprojectnameContainswsgifile>.wsgi. – Aslesha Kantamsetti Commented Feb 16 at 1:02
  • Share your settings.py file. – Aslesha Kantamsetti Commented Feb 16 at 1:02
  • If possible, share your GitHub repo here. – Aslesha Kantamsetti Commented Feb 16 at 1:03
  • How are you deploying your app to Azure using GitHub or VS Code? – Aslesha Kantamsetti Commented Feb 16 at 1:05
  • Have you added gunicorn to requirements.txt file ? – Sirra Sneha Commented Feb 17 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 0

keep getting a 502 Bad Gateway error.

You're getting a bad gateway error because gunicorn isn't installed correctly.

  • Add gunicorn to your requirements.txt file and make sure it’s installed.
  • Make sure your wsgi.py file correctly sets the DJANGO_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.middlewaremon.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:

本文标签: pythonDjango Project on Azure502 error after deploymentStack Overflow