Fix Dokploy bad gateway issue

- Fix ALLOWED_HOSTS configuration in dokploy.json (set to *)
- Update production_settings.py to respect SECURE_SSL_REDIRECT environment variable
- Change startup script to use netcop_hub.production_settings instead of production_https_settings
- Add proper environment variable handling for SSL and CSRF settings
- Resolve SSL redirect conflicts between Dokploy proxy and Django settings
This commit is contained in:
thecyberlearn 2025-09-05 10:10:02 +05:30
parent a08cc04250
commit 5b4bc80b03
3 changed files with 40 additions and 21 deletions

View File

@ -7,20 +7,16 @@
"buildArgs": {},
"buildOptions": ["--no-cache"],
"security": {
"redirectHttpsToHttp": false,
"forceHttps": true
},
"traefik": {
"tls": true,
"certResolver": "letsencrypt"
"redirectHttpsToHttp": true,
"forceHttps": false
},
"env": {
"PYTHONUNBUFFERED": "1",
"DEBUG": "false",
"SECRET_KEY": "production-key-change-this-123456789",
"ALLOWED_HOSTS": "website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me,*",
"ALLOWED_HOSTS": "*",
"DOKPLOY_PROJECT_NAME": "quantum-tasks-ai",
"SECURE_SSL_REDIRECT": "true",
"SECURE_SSL_REDIRECT": "false",
"SECURE_PROXY_SSL_HEADER": "HTTP_X_FORWARDED_PROTO,https",
"CSRF_TRUSTED_ORIGINS": "https://website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me,http://website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me"
}

View File

@ -8,7 +8,12 @@ import os
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')
# Parse ALLOWED_HOSTS from environment
allowed_hosts_str = os.environ.get('ALLOWED_HOSTS', '')
if allowed_hosts_str:
ALLOWED_HOSTS = [host.strip() for host in allowed_hosts_str.split(',') if host.strip()]
else:
ALLOWED_HOSTS = []
# Database
DATABASES = {
@ -25,16 +30,33 @@ DATABASES = {
# Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Security settings
SECURE_SSL_REDIRECT = True
# Security settings - respect environment variables for Dokploy compatibility
SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'true').lower() == 'true'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# Only enable secure cookies and HSTS if SSL redirect is enabled
if SECURE_SSL_REDIRECT:
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
else:
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False
# Trust proxy headers for Dokploy/Traefik
USE_X_FORWARDED_HOST = True
USE_X_FORWARDED_PORT = True
# CSRF trusted origins from environment
csrf_origins_str = os.environ.get('CSRF_TRUSTED_ORIGINS', '')
if csrf_origins_str:
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in csrf_origins_str.split(',') if origin.strip()]
else:
CSRF_TRUSTED_ORIGINS = []
# Logging
LOGGING = {

View File

@ -8,23 +8,24 @@ echo "======================================="
# Run database migrations
echo "📄 Running database migrations..."
DJANGO_SETTINGS_MODULE=production_https_settings python manage.py migrate --noinput
DJANGO_SETTINGS_MODULE=netcop_hub.production_settings python manage.py migrate --noinput
# Create cache table if needed
echo "🗄️ Ensuring cache table exists..."
DJANGO_SETTINGS_MODULE=production_https_settings python manage.py createcachetable || true
DJANGO_SETTINGS_MODULE=netcop_hub.production_settings python manage.py createcachetable || true
# Check if we can access the database
echo "🔍 Testing database connection..."
DJANGO_SETTINGS_MODULE=production_https_settings python manage.py check --database default
DJANGO_SETTINGS_MODULE=netcop_hub.production_settings python manage.py check --database default
# Start the application
echo "🌐 Starting gunicorn server on port 3000..."
echo "Health check endpoint: /health/"
echo "🔒 HTTPS enabled with SSL redirect"
echo "🔒 SSL redirect: $SECURE_SSL_REDIRECT"
echo "🌐 Allowed hosts: $ALLOWED_HOSTS"
echo "======================================="
DJANGO_SETTINGS_MODULE=production_https_settings exec gunicorn netcop_hub.wsgi:application
DJANGO_SETTINGS_MODULE=netcop_hub.production_settings exec gunicorn netcop_hub.wsgi:application
--bind 0.0.0.0:3000 \
--workers 2 \
--timeout 120 \