Django Sites Framework: Managing Multi-Domain Content with SITE_ID and Nginx

Django's built-in django.contrib.sites framework allows a single PostgreSQL database and codebase to power multiple distinct websites—associating articles, users, and configurations with one or more Site records via ForeignKey or ManyToManyField.

1. Dynamic Site Resolution Without Multiple Settings Files

While the traditional approach uses separate settings_site1.py and settings_site2.py files with hard-coded SITE_ID constants, modern containerized deployments on Google Cloud Run or Kubernetes benefit from resolving the active Site dynamically via get_current_site(request):

from django.contrib.sites.shortcuts import get_current_site
from blog.models import Article

def site_articles_view(request):
    current_site = get_current_site(request)
    articles = Article.objects.filter(sites=current_site, published=True)
    return render(request, "blog/list.html", {"articles": articles, "site": current_site})

2. Nginx Reverse Proxy Headers

Ensure Nginx or your Cloud Load Balancer forwards the exact host and scheme so Django resolves the right Site.domain and generates canonical HTTPS URLs:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs