Where to set up subdomain routing? Front-end (React) or Django (Backend)?

I'm currently building a multi-tenant web application using Django and React. Right now, each user's page is available at a URL like https://mywebsite.com/. However, I want to change this so that each user has their own subdomain, for example: https://username.mywebsite.com.

My setup is as follows: I'm using React for the frontend and Django (with Django REST Framework) for the backend. Both are running on the same server. I’m serving the React build/ folder directly through Django by connecting it in the Django urls.py file.

What I want to achieve is true subdomain-based routing so that each user or tenant can have their own subdomain. I’m not sure exactly what changes are needed to make this work, especially across the different layers of the stack. Specifically, I’m looking to understand what needs to be done at the DNS or Nginx/server configuration level, what changes I need to make in Django to detect and handle subdomains, and whether any updates are needed on the React side. I’m also wondering whether there’s a way to test this kind of subdomain setup locally during development (like on localhost).

Finally, I'd like to know how to extract the subdomain (which would be the username or tenant identifier) in Django and route the request accordingly.

If anyone can guide me or point me in the right direction, that would be a huge help.

Subdomain resolutions are a matter for the DNS, not the backend/frontend.

But you can add a *.yourdomain.com and handle the logic server-side

In that case, your question was already answered here Using subdomains in django

1. DNS level approach

In simple terms, a domain is just a human-readable version of an IP address. An IP address, in turn, points to a server on the network. Generally, different domains lead to different servers, and this also applies to subdomains. Here's an example:

stackoverflow.com → 10.1.1.1
meta.stackoverflow.com → 20.1.1.1

But from what I understand, it looks like all subdomains are required to point to a single server(=your django/react server)

user1.yourservice.com → 30.1.1.1
user2.yourservice.com → 30.1.1.1

This is technically possible, but it may not be appropriate. The reason is that most DNS services like AWS Route53 impose limits on the number of subdomains you can create. So if you have a large number of users who each require a subdomain, it would be difficult to assign one to every user.

Also, whenever a new user signs up, you would need to register a new subdomain with your DNS provider — a task that cannot be handled at the Django level.

2. Web server level approach

However, if needed, the web server(like Nginx) should handle mapping subdomains to specific subdirectories. The specific Nginx configuration may vary, so please take this only as a conceptual idea.

# main service
server {
    listen 80;
    server_name localhost;

location / {
    root   /path/to/your/django/project/;
    proxy_pass   http://yourservice.com:8000;
    }
}

# for each user
# This configuration is used to link subdomains to individual user pages, such as profile pages.

server {
    listen 80;
    server_name ~^(.*)\.yourservice\.com; # user1.yourservice.com

location / {
    proxy_pass http://yourservice.com/$1; # to yourservice.com/user1
    }
}

# Or, And in this case, each user can access the service entirely through their own subdomain.

server {
    listen 80;
    server_name ~^(.*)\.yourservice\.com;

location / {
    proxy_pass http://yourservice.com;
    }
}

3. Web application level approach

I don't know your specific scenario — whether only certain pages for each user (like profile pages) need to be redirected to a subdomain, or whether every user should access the service entirely through their own subdomain.

However, since you're using subdomains, there will be some configuration options you need to set. For example, you may need to add the subdomains to ALLOWED_HOSTS or CSRF_TRUSTED_ORIGINS in settings.py. You might also need to update links in your templates accordingly. These details will depend on your specific scenario.

4. Subdomain setup for local development

By configuring the hosts file, you can access your localhost using (sub)domains. On Windows, the hosts file is located at C:\Windows\System32\drivers\etc\hosts, and on macOS, it's at /private/etc/hosts. It should work if you modify it like this.

127.0.0.1   yourservice.com
127.0.0.1   user1.userservie.com
127.0.0.1   user2.userservie.com
...

The concept of mapping a subdomain to an IP address in your local hosts file is essentially the same as doing it through DNS. However, for various reasons, automating this process in Django—such that it happens every time a user is created—would likely be difficult.

I hope this answer helps you revise or clarify your scenario.

Вернуться на верх