Developers May 26, 2020

New subdomain for each account?

Damon @damon

Say I have a domain example.com. For each user, I want to dynamically create a subdomain for them. Say for user1, it will have user1.example.com. For user2, it will have user2.example.com.

I am familiar with Firebase, but it doesn't seem to provide this multi-tenant hosting services.

What hosting providers are you using? How do you create subdomains for your saas product?

Update: I will be using Vercel for hosting. Wildcard subdomains and SSL Certs are enabled by default. Soooo cooool!!!

  1. 5

    I'm using vercel for this. I set up a wildcard to point to my site they host. Then all subdomains automatically go to my site. I don't need to add records when I get a new user.

    1. 1

      That's cool. I didn't even know that was a thing you could do.

    2. 1

      Does it create a new subdomain for each new user?

      1. 2

        It is set up so that any subdomain that isn't mapped to something else will automatically go to your site. So when someone signs up I just record what subdomain they registered and there isn't any need for dns changes.

        1. 1

          I moved hosting to Vercel. What a great product! I even got the Github integration that Firebase doesn't even support. Lots of cool things. Thanks Brent!!!

        2. 1

          This is so cool Brent. Would like to dig more in Vercel.

  2. 4

    I know this is off topic from your original post, and definitely something you might have considered already, but it's worth thinking about the implications this could have on your site's SEO architecture. If you plan on producing a product that in someway benefits from search engine traffic and/or users link to their URLs from other sites then you should probably reconsider.

    Subdomains work extremely well at sectioning off content within a domain and search engines take a strict view on this. Sometimes it can be beneficial. For instance if you were Disney you wouldn't want your Marvel merchandise pages conflicting with your Disney World pages, so you could separate them to marvel.disney.com and parks.disney.com. Search engines would view them as separate and not confuse the content.

    Unfortunately this also means that any links marvel.disney.com gains have no impact on parks.disney.com's search engine rankings. If you didn't already know, people linking to your site is the basis for how search engines decide which site should rank first. The more links, the higher the ranking. If you split your site into subdomains, you're telling search engines that links to each subdomain should transfer no 'authority', as SEO professionals call it, to any other subdomain.

    To get around this, it's recommended that sites which want to optimise for search engines should use subdirectories instead. That way when your site is linked to, be it to yoursite.com/user1 or yoursite.com/user2, all of the 'authority' is passed around evenly. This allows your site to grow it's search engine presence easily and effectively capitalise on any inbound links.

    If optimising for search engines is in your roadmap, I'd also recommend checking out my new project Keywords Fast. We create detailed analysis of what your users are searching for, and give you clear tips and tactics on how to capitalise on it.

    1. 1

      Thank you so much Alex! I am not a SEO expert. And this is the 1st time I know subdomains not bring positive SEO for the apex domain👍

      1. 1

        No worries, I'm glad it was helpful!

  3. 3

    I am actually doing this with an app of mine: https://strands.io/

    i.e. https://tomnook.strands.io/

    I used Let's Encrypt for ssl certs and configured nginx to proxy the requests to a next.js app which takes care of checking for a user with the specified username.

    Happy to discuss further if you need any assistance!

    1. 1

      Thanks Nick! How do you create subdomain on the fly whenever a new user signed up?

      1. 2

        Just noticed my first comment may have been misleading, I am using a wildcard subdomain certificate.

        With this, adding a new subdomain is just adding a new entry to my database.

        1. 1

          That makes sense. And it's seems to be the quickest and easiest solution👍

          1. 2

            Yeah, I would say so.
            There's also something to be said about the overhead having to manage all of the certs if you were planning to scale to multiple hosts and all of that.

            If the above meets your requirements, I think it's worth a try!

            1. 1

              Definitely worth a try for the MVP. Many thanks!

  4. 3

    The easy way: You'll need to use Route 53 (Amazon), Cloud DNS (Google Cloud) or any other similar service from your cloud provider to achieve that. Creating A records may take quite a long time to propagate which you don't want on signup. I assume if you're using firebase then Cloud DNS is the way to go. (https://firebase.google.com/docs/hosting/custom-domain)

    If you don't care about "reserving" them for real or balance the load based on those domains, you can just create a wildcard A record *.example.com (https://support.google.com/domains/answer/4633759?hl=en) and point it to a web server. You can then redirect your traffic based on the config of your webserver as you need. In this scheme, I highly recommend you to have a separate webserver for routing traffic and have app webservers separately.

    1. 1

      Thank you Sergey! You are right. Propagation of A record has too much latency. Cloud DNS by GCP may be the best option. I has the API to dynamically add A record https://cloud.google.com/dns/records.

      But once I use Cloud DNS, I think I cannot use Cloudflare for my CDN. Not sure if I will sacrifice some performance by doing so.

      1. 2

        It seems that Cloudflare is having their DNS API as well. Not sure about how fast it propagates changes and what are the requirements to use it. It seems to be free, so worth a look.

        1. 1

          I think I will try Cloud DNS first. Thanks Sergey for helping out here👍

  5. 2

    Not, sure firebase is linked to the Domain Name, doesn’t Google Apps offer something like domain name management? It should be the process of automatically creating a new A record as part of the process of onboarding the new user. Like onboard user => trigger function => creates ‘newUser.myCoolDomain.com’ A record => ???
    The ??? Is whether or not to give the newUser a copy of the top level application or the illusion they have their own copy of it for their experience.

    1. 1

      Thanks! It really helps! I got the domain from namecheap, and added A record for pointing to Firebase hosting IP. Firebase doesn't have DNS data to let you add A record. Checking namecheap, it has the API for developers to add dns. I am digging into that!
      https://www.namecheap.com/support/api/methods/domains-dns/set-hosts/

  6. 1

    Hi Damon, just discovered this thread. I'm considering moving my product to Vercel. Do you know if it is possible to setup CNAME ie. customer sub domain pointing to the domain name offered by the product? If yes, can you share some ideas.

    1. 1

      Hey Wilson, sorry for the late reply.

      Yes you can do that!

      Set an alias record to point cname.yourdomain.com to Vercel, then ask you client to set up a CNAME record to point to cname.yourdomain.com.

      Of course you can ask your client to point to Vercel's, but that doesn't look professional.

      1. 1

        Thanks Damon. Will give this a try.

        1. 1

          @damon I gave this a try. I see the resolution happens fine. However it throws up an error saying 'deployment not found'. Upon checking this with Vercel, they are suggesting to add the customer domain to the project. Is that how it works? Adding that has a limitation of just 50 per project. Would like to know about how backlogs.co is solving this? Can you help?

          1. 1

            Hey Wilson, yes render has 50 limit unless you are in their enterprise plan.

            I later moved to Render, there is no limit there.

  7. 1

    We've kept it simple on our end. We have a wildcard SSL certificate that points to our server, and then a wildcard A record. So any *.domain.com request gets pointed to the same place.

    This is a common pattern for multi-tenant applications; whether the same DB or different.

    If it's the same database, you can have a table for domains which stores all your domains, and you scope your queries with that domain_id every time.

    If it's different databases, you need to add some sort of middleware in your request that defines which database to query based on domain.

    For scaling purposes, as you scale, you can duplicate the logic with a load balancer.

    1. 1

      Yep Hassan. That is the approach I am trying to achieve. For my case, Firebase does neither support wildcard ssl certs, nor the wildcard subdomains. I will find some alternatives for this purpose. If you don't mind, would you like to share what hosting are you using, and what ssl certs? Thanks!

      1. 2

        Hey! Sure thing. For our current use case, we're using DigitalOcean for hosting, and LetsEncrypt to provision SSL certs.

        Since our app is PHP based, we use Laravel Forge to manage our servers so it easily takes care of SSL for us using their GUI. It handles auto renews as well.

        If you're looking beyond PHP though, I can recommend https://ploi.io/ for server management. You are free to choose your own cloud provider, and they will handle the SSL cert installation for you.

        You typically want your platform to be cloud agnostic for as long as possible. Feel free to message me if you have any specific questions!

        1. 1

          Thank you so much Hassan! It's really helpful!

  8. 1

    This is how we manage ours and all hosted on a VPS provided by Digital Ocean.

    The biggest problems we had were managing Wildcard SSL Certificates from Lets Encrypt (need manual verification) and setting up the configs for subdomains on demand for apache.

    Once we passed this hurdle it has been a lot easier as we manage the whole pipeline it is easy to replicate this locally for testing or on other VPS providers if we ever consider moving.

    1. 1

      Thank you Chris! In my case, the SSL is bundled with the Firebase hosting, and I think they internally use Lets Encrypt. So setting up SSL isn't a problem for me. The only hurdle is to set up the wildcard subdomain in firebase hosting. Looks like they don't support this feature. Bummer!

Recommended Posts