21
7 Comments

How do I setup a domain to subdomain architecture like Shopify?

We have got a multi-tenant architecture ready with Laravel Framework (i.e when a new users register a new subdomain is created and it's working fine)

Now if the user(hello) wants to access his account, they can go to hello.oursaas.com and see their website.

But how do they achieve the same if they want their own website "hello.com" (not a subdomain), to be pointed to our hello.oursaas.com??
(That means when any user goes to hello.com, the address bar shows hello.com, but the content is of hello.oursaas.com)

How does Shopify do this? (I have a bit idea)

  • they ask their users to change the A record to point to the Shopify's IP (we can do the same)

  • they also ask the users to create a CNAME record of WWW and point to shop.shopify.com(this is where we are not able to understand what is happening behind this layer)

Any insights on this will be useful.

posted to Icon for group Developers
Developers
on October 13, 2020
  1. 4

    I had a very similar setup on Kampsite and ended up using CNAME's (mainly because my users would host it on a subdomain on their domain). So basically I created a cname record on my DNS e.g. cname.kampsite.co and would have users point a CNAME there e.g. feedback.theirapp.com. If a user was using their root domain e.g. theirapp.com then it would be an A record.

    The next thing you need to figure out though would be SSL certificates. Since I had them point to my netlify account, I could programatically add their domain to the site that hosted cname.kampsite.co which would automatically generate an SSL certificate for them, otherwise you could use something like lets encrypt manually yourself OR via something like caddy.

    1. 3

      @hlucas pretty much nailed it, though I do want elaborate a bit on A records vs CNAMEs:

      they ask they user to change the A record to point to the Shopify's IP (we can do the same)

      The reason Shopify provides an IP to create an A record rather than a CNAME is because technically you cant CNAME the root of a domain (e.g. "example.com") to another domain [1]. Some DNS providers may allow you to do so, but you'll likely run into problems down the road; email service providers seem to be very particular about it from my experience.

      Just make sure that if you DO provide an IP, that it is static and that you can keep in indefinitely!

      they also ask the users to creare a CNAME record of WWW and point to shop.shopify.com(this is where we are not able to understand what is happening behind this layer)

      Under the hood this is the same as them asking you to create an A record - only difference is that since www is a subdomain and not the root, it can be a CNAME record. Somewhere in their loadbalancer/stack they're looking at the Host header of the http request to know where to route things to and if www.yourstore.com maps to a shop in their system.

      [1] https://www.freecodecamp.org/news/why-cant-a-domain-s-root-be-a-cname-8cbab38e5f5c/#cname-limitations

  2. 4

    This is a great question and one that we had to solve last year too.

    Our product is a hosted CMS, so it's probably a similar infrastructure concept as yours.

    Our app has a specific IP address (we in fact have two because we use AWS Global Accelerator https://aws.amazon.com/global-accelerator/) so we have them point the A record for their apex domain to our primary IP.

    We have a subdomain (sites.branchcms.com) set up that is a CNAME for the AWS Global Accelerator subdomain. That essentially also uses the same IP that the apex domain A record would use, but where Global Accelarator gives you two IPs, it uses both IPs as fallbacks.

    Here are the instructions we provide to customers. https://www.branchcms.com/learn/docs/overview/dns

    You could, of course, have them set both the apex domain and the "www" subdomain as an A record pointing to your IP address. We chose to have "sites.branchcms.com" and have them do a CNAME record because it gives us a brandable record and also allows us to make any changes to the DNS for sites.branchcms.com if we need to.

    I hope that helps. If not, let me know.

  3. 1

    This is exactly the problem I am struggling with for my wifes project knoww.me . I don't have better answer than others, but I made some progress with CaddyServer hosting multiple domains. This only working with static sites and a bit headache to deal with, so I am searching for any providers who can simplify this process. ex: https://tinny.host

      1. 1

        Thank you fly.io is interesting.

  4. 1

    This comment was deleted 5 years ago.

Trending on Indie Hackers
This Week in AI: The Gap Is Getting Clearer User Avatar 49 comments 1 small portfolio change got me 10x more impressions User Avatar 28 comments AI Is Destroying the Traditional Music Business and Here’s Why. User Avatar 24 comments Fixing my sleep using public humiliation and giving away a Kindle User Avatar 21 comments A Tiny Side Project That Just Crossed 100 Users — And Somehow Feels Even More Real Now User Avatar 13 comments From 1k to 12k visits: all it took was one move. User Avatar 11 comments