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": {}, "buildArgs": {},
"buildOptions": ["--no-cache"], "buildOptions": ["--no-cache"],
"security": { "security": {
"redirectHttpsToHttp": false, "redirectHttpsToHttp": true,
"forceHttps": true "forceHttps": false
},
"traefik": {
"tls": true,
"certResolver": "letsencrypt"
}, },
"env": { "env": {
"PYTHONUNBUFFERED": "1", "PYTHONUNBUFFERED": "1",
"DEBUG": "false", "DEBUG": "false",
"SECRET_KEY": "production-key-change-this-123456789", "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", "DOKPLOY_PROJECT_NAME": "quantum-tasks-ai",
"SECURE_SSL_REDIRECT": "true", "SECURE_SSL_REDIRECT": "false",
"SECURE_PROXY_SSL_HEADER": "HTTP_X_FORWARDED_PROTO,https", "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" "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! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False 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 # Database
DATABASES = { DATABASES = {
@ -25,16 +30,33 @@ DATABASES = {
# Static files # Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Security settings # Security settings - respect environment variables for Dokploy compatibility
SECURE_SSL_REDIRECT = True SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'true').lower() == 'true'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
# Only enable secure cookies and HSTS if SSL redirect is enabled
if SECURE_SSL_REDIRECT:
SECURE_HSTS_SECONDS = 31536000 SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
CSRF_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
LOGGING = { LOGGING = {

View File

@ -8,23 +8,24 @@ echo "======================================="
# Run database migrations # Run database migrations
echo "📄 Running 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 # Create cache table if needed
echo "🗄️ Ensuring cache table exists..." 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 # Check if we can access the database
echo "🔍 Testing database connection..." 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 # Start the application
echo "🌐 Starting gunicorn server on port 3000..." echo "🌐 Starting gunicorn server on port 3000..."
echo "Health check endpoint: /health/" echo "Health check endpoint: /health/"
echo "🔒 HTTPS enabled with SSL redirect" echo "🔒 SSL redirect: $SECURE_SSL_REDIRECT"
echo "🌐 Allowed hosts: $ALLOWED_HOSTS"
echo "=======================================" 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 \ --bind 0.0.0.0:3000 \
--workers 2 \ --workers 2 \
--timeout 120 \ --timeout 120 \