Backend & Cloud Engineer Guide: Multi-Domain & Multi-Tenant SaaS Architecture with the Django Sites Framework, Nginx & Cloud Run
When a single backend platform serves multiple branded domains or regional country portals (e.g., `aldianfazrihady.com`, `aldianapps.com`, or custom white-label enterprise domains), running separate database clusters and duplicate container fleets wastes cloud budget. As a Senior Backend Engineer and Cloud Engineer, I use the Django Sites framework (`django.contrib.sites`) paired with dynamic host-header resolution on Google Cloud Run and Nginx to serve multiple domains from a unified codebase.
1. Dynamic Per-Request `Site` Resolution Middleware vs. Static `SITE_ID`
Instead of hardcoding `SITE_ID = 1` in `settings.py` and spinning up separate gunicorn processes per domain, resolve the active `Site` dynamically from the validated `Host` header (cached in process memory via `SITE_CACHE`):
from django.contrib.sites.models import Site
from django.http import HttpRequest
def get_current_tenant_site(request: HttpRequest) -> Site:
"""
Resolves and caches the Django Site record from the validated HTTP Host header.
"""
host = request.get_host().split(":")[0].lower()
return Site.objects.get_current(request)
2. Strict `ALLOWED_HOSTS`, `CSRF_TRUSTED_ORIGINS` & Canonical SEO Isolation
Preventing Host-Header Poisoning: Configure Nginx and Cloud Run custom domain mappings so only verified domains reach the WSGI/ASGI application, and always generate `` and password-reset URLs against the verified canonical domain.
⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer
Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:
About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)
Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.