同一Django项目下应用:子目录还是子域名方案选型咨询
Hey there! Let’s walk through your options clearly since you’re working with a Django project hosting 6 apps, sharing Google Auth sessions, and planning to scale over the coming months. I’ll break down both approaches, their pros/cons, and which might fit your needs best.
Subdomain Approach (Using Django Sites Framework)
This is the "official" Django way to handle multiple domains/subdomains under one project:
Setup Requirements:
- Enable the
django.contrib.sitesapp inINSTALLED_APPS, then configureSITE_IDand populate thesitesdatabase table with entries for each subdomain (e.g.,app1.yourdomain.com,app2.yourdomain.com). - Update
ALLOWED_HOSTSto include all your subdomains (or use a wildcard like*.yourdomain.comfor simplicity). - For shared sessions, set
SESSION_COOKIE_DOMAIN = '.yourdomain.com'insettings.py(the leading dot ensures cookies are shared across all subdomains of your main domain).
- Enable the
Google Auth Considerations:
- You’ll need to add each subdomain’s callback URL to your Google Cloud OAuth2 client configuration (e.g.,
https://app1.yourdomain.com/auth/google/callback). Some OAuth providers support wildcard callbacks, but double-check Google’s current restrictions—you might need to list each one explicitly.
- You’ll need to add each subdomain’s callback URL to your Google Cloud OAuth2 client configuration (e.g.,
Deployment with Nginx/Gunicorn:
- Create separate
serverblocks in your Nginx config for each subdomain, all proxying requests to your Gunicorn instance. For example:server { server_name app1.yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; # Other proxy headers here } } - Supervisor will still manage your single Gunicorn process (or multiple for load balancing) as usual.
- Create separate
Pros:
- Clean, isolated branding for each app (each feels like a separate service).
- Easier to split individual apps into separate servers/containers later if scaling requires it—you can just point a subdomain’s Nginx config to a new Gunicorn instance without reworking routing.
- Better for SEO if you want each app to be treated as a separate site.
Cons:
- More initial setup overhead (managing the Sites table, updating ALLOWED_HOSTS, configuring multiple OAuth callbacks).
- Requires a wildcard SSL certificate (or individual certs for each subdomain) to cover all endpoints.
Subdirectory Approach
This is a simpler setup where all apps live under paths like yourdomain.com/app1/, yourdomain.com/app2/:
Setup Requirements:
- No need for the Sites framework—just mount each app’s URLs into your project’s root
urls.pyusinginclude():# project/urls.py from django.urls import path, include urlpatterns = [ path('app1/', include('app1.urls')), path('app2/', include('app2.urls')), # ... other apps ] - Sessions are shared automatically by default since all paths are under the same domain—no need to tweak
SESSION_COOKIE_DOMAIN.
- No need for the Sites framework—just mount each app’s URLs into your project’s root
Google Auth Considerations:
- Only one OAuth callback URL is needed (e.g.,
https://yourdomain.com/auth/google/callback), since all apps use the same domain. This cuts down on configuration work significantly.
- Only one OAuth callback URL is needed (e.g.,
Deployment with Nginx/Gunicorn:
- A single Nginx
serverblock works for all apps—just proxy all requests to Gunicorn, and Django handles the subdirectory routing:server { server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; # Other proxy headers here } }
- A single Nginx
Pros:
- Minimal initial setup—no extra database tables or complex configs.
- Google Auth configuration is a one-and-done task.
- Easier to scale the entire project horizontally (adding more Gunicorn instances behind a load balancer) since all traffic goes through a single domain.
Cons:
- Less isolation between apps—splitting an app into a separate service later would require rewriting URLs and adjusting Nginx/Django routing, which is more work.
- URLs are longer and less "clean" for end users (e.g.,
yourdomain.com/app1/dashboardinstead ofapp1.yourdomain.com/dashboard). - SEO treats all apps as part of a single site, which might not be ideal if you want each app to have its own search presence.
Recommendation for Your Scenario
- Go with subdomains if: You anticipate needing to split individual apps into separate services down the line (e.g., different teams maintaining each app, or varying resource requirements per app). The upfront setup work pays off for long-term flexibility.
- Go with subdirectories if: Your apps are tightly coupled, you don’t plan to split them apart, or you want to keep configuration as simple as possible. This is the low-friction choice that works great for shared auth and unified scaling.
Either way, your existing deployment stack (Django > Gunicorn + Supervisor > Nginx) will work seamlessly—you just need to adjust the Nginx config and Django settings accordingly.
内容的提问来源于stack exchange,提问作者Rakmo

