Django get sub domain from which the request was sent from

Viewed 102

i have a standalone front ends serving on different sub domains, and a single backend responding to all requests through API(s)

Say i have following subdomains

first.example.com
second.example.com
third.example.com

and my backend is at

backend.example.com

in my backend views i want to know which subdomain sent me request

i tried

self.request.get_host().split('.')[0]

but this gave me "backend" as result from every sub-domain

what i want to have :

  • if request was sent from first.example.com > in my backend.example.com view i want to have "first"

  • if request was sent from second.example.com > in my backend.example.com view i want to have "second"

and so on

Technologies information:

  • Subdomains using React
  • Backend using Django
  • Server Nginx
  • other server Gunicorn
1 Answers

You can get subdomain name from HTTP_REFERER

from urllib.parse import urlparse

urlparse(request.META.get('HTTP_REFERER')).hostname.split('.')[0]
Related