Question

How to setup subdomain for tenants in Django app running in droplet

thanks in advance. I have a Django app using Django-tenants. The app runs well locally but I haven’t been able to solve the subdomain for tenants. So far:

  1. I have a public and a test schema with superusers created.
  2. I tried all combinations os allowed host, *.mydomain.com, ip, *.www.mydomain.com, now I’m testing with ‘.*’, ‘*
  3. I have a wildcard SSL certificate installed in the server.
  4. Currently I have an A record with * pointing to my droplet ip, also tried CNAME.
  5. So far I can only access to the top domain of the app, and not to subdmains.
  6. Here is the relevant code/conf.
server {
    listen 80;
    server_name *.mydomain;
    location / {
        rewrite ^ http://mydomain;
    }
}

server {
    listen 443 ssl http2;
    server_name *.mydomain.com;

    ssl_certificate /etc/nginx/ssl/mydomain_com_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;

    # SSL/TLS security settings
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDxxxxxxxxxxxxxxxxxx';

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location /staticfiles/ {
        root /root/project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Hope someone can help me out. Thanks!!

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 13, 2023
Accepted Answer

Hello there!

The setup for the Django-tenants library using subdomains can be a bit tricky indeed.

You mentioned that you have already tried various configurations which sounds correct, what is the exact error that you currently get when you visit a tenant’s subdomain?

Let’s look at the configuration of Nginx and the Django settings:

  1. Nginx Configuration: Make sure that the server_name directive in the Nginx configuration file correctly captures all subdomains:
server {
    listen 443 ssl http2;
    server_name *.mydomain.com mydomain.com;

    # ...
}

The server_name should be set to *.mydomain.com and mydomain.com so that it can serve both the root domain and any subdomains.

  1. Django settings.py: For your ALLOWED_HOSTS in your settings.py file, try using ['.mydomain.com', 'mydomain.com']. The leading dot allows all subdomains.

  2. Django-tenants: Make sure you have correctly set up the Django-tenants middleware. In your settings.py, you should have something like:

MIDDLEWARE = [
    ...
    'django_tenants.middleware.main.TenantMainMiddleware',
    ...
]

Ensure that your TENANT_MODEL is correctly set too.

  1. Check your tenants: Make sure that your tenants have been set up correctly with the right domain_url value.

Lastly, consider checking your logs for any error messages. Django’s logs can be very helpful, and so can Nginx’s. You can view the Nginx error logs with sudo tail -f /var/log/nginx/error.log.

UPDATE: The issue was related to DNS propagation and it was working fine on the next day!

Let me know how it goes!

Best,

Bobby

thanks for the quick response. Today is working 😃, so I guess it was just a slowed dns propagation problem (just my thoughts on this). Just in case some has problems with this library I had to migrate all apps in shared apps and then move te tenants ones to tenant, otherwise I kept having migration problems. Thanks Bob!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel