DEV Community

ADONIS SIMO
ADONIS SIMO

Posted on

How to create sub-domain automatically when users register on my SaaS application ?

Hi, am currently working on a SaaS product and i will need to create sub-domain on fly as users register on the app. I think this operation is related to the DNS provider or the hosting service, via an API for example. But i don't really know how it work. Looking for some help on this.

thanks

Top comments (16)

Collapse
 
matthew_collison profile image
Matthew Collison

You wouldn't create the subdomain on the fly, you'd simply use a wildcard subdomain (*.yourwebsite.tld) and point that to your webserver.

Depending on what backend framework you are using, you'd need to ingest the subdomain as a parameter and use the value from that to find the user (or throw a 404, of course)

Hope that helps!

Collapse
 
dreamindani profile image
Dani Sandoval

Yup! This is essentially what anymessage does to generate the team subdomains. Feel free to checkout the code, if you'd like a node.js example: github.com/d3sandoval/anymessage

Collapse
 
simo97 profile image
ADONIS SIMO

oww.... that's interesting... i only knew about SSL wildcard certificat... this is sooo cool. Thank it solve my problem, am working with a NGINX + Gunicorn + Django Stack.. i can handle the domain name ingestion without problem... thank you for this.

Collapse
 
matthew_collison profile image
Matthew Collison

Super welcome - glad it helped!

Collapse
 
kosadaz profile image
Kosadaz

Hi Matthew,

Better to have single VM and single db running for each tenant? Or multitenant single db if that the case?

ie. Slack use subdomain with single db or multitenant db?

Collapse
 
surajcreator profile image
surajcreator • Edited

Wow. That was super easy solution. Thanks buddy.

But Where do I deploy my application? Will it be on main domain?

Collapse
 
chr314 profile image
Christoforos Aslanov

if you are using cloudflare they have rest api api.cloudflare.com

with simple http requests you can manage your dns records

this way you can have multiple servers

Collapse
 
simo97 profile image
ADONIS SIMO

Ha... I had this kind of solution in mind.. thanks for this. I will check that, it seem to be more suitable concerning security.

Collapse
 
chr314 profile image
Christoforos Aslanov

yeah cloudflare is great, in the free package they give you tools like waf, ddos protection, cdn, js css optimization etc and your server ip is not exposed to the public because cloudflare works as reverse proxy

Collapse
 
eblucas profile image
eblucas

Hi guys,

I have a similar problem but I would like to give the option to the user use their domain as well. Is is possible? Like wix give you the opinion to use your domain for a e-commerce for example.

Any help appreciated.

Collapse
 
ipoool profile image
Asep saepulloh

hi @eblucas have you found the answer????

Collapse
 
kalium profile image
kalium.xyz

You can get wildstar certificates for TLS on AWS, if you want to have a secure connection while serving any subdomain.

Collapse
 
simo97 profile image
ADONIS SIMO

yeah thank you, i knew about this type of certificate, i use to get them from GlobalSign.

Collapse
 
marvinschopf profile image
Marvin

You could easily get them for free from Letβ€˜s Encrypt.

Collapse
 
ikennaobieze profile image
Paschal Jeremy • Edited

You have gotten lots of advice already but I feel an important part is left out - using a load balancer.

You can configure Nginx to handle this then instead of calling Cloudflare API as suggested above, just point the wildcard to Nginx.
From there, you can do whatever you want with the resulting traffic - authenticate it, redirect to another microservice - anything you want.

Collapse
 
bytephase profile image
Bytephase Technologies Pvt Ltd

At Bytephase Repair Shop Software, we've executed a similar implementation where we assign each customer a unique subdomain and an independent database, facilitated by tenancyforlaravel.com/. This strategy has proven advantageous, as it offers our customers a personalized workspace, instilling a sense of ownership and nurturing trust. We take pride in the successful execution of this initiative.
More on challenges we faced bytephase.com/blog/subdomain-for-c...