3

I am working on a MEAN stack SAAS application where I want to provide each user with their own unique subdomain. For example, I want the user John Doe to have the following subdomain to his name: johndoe.website.com.

The application is nearly ready pending this feature. I am looking for steps to accomplish this using NGINX and Node js in a manner that will lead to minimal changes to our existing code base.

I have searched the internet, and I was unable to find a resource that can serve as a step-by-step guide to help me solve this problem.

I know that the solution would involve the creation of Server Blocks to handle wildcard subdomains using NGINX, but I am not sure about the exact steps that will have to be implemented to get this done.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    You will need a DNS system that you can control programmatically to create subdomains. I used to run Bind myself and I was able to easily manage subdomains because they are just text files. If you don't want to run your own DNS server and want to use a DNS hosting provider then you will need one that provides an API to create/edit/delete subdomains like Amazon's Route 66 – slebetman Apr 15 '20 at 10:17
  • how about creating a file and then reloading Nginx? Note reload and restart are different things. – mehta-rohan Apr 15 '20 at 10:21
  • AWS route 53 does have an API. But based on what I have read, things get hard to manage past 500 or so subdomains as you have to seek permission from AWS to generate more subdomains. An NGINX based approach will work better in my case – Manvendra Singh Apr 15 '20 at 11:19
  • @mehta-rohan: Could you please explain it in some detail? – Manvendra Singh Apr 15 '20 at 11:20
  • https://serverfault.com/questions/249952/wildcard-vhosts-on-nginx – mehta-rohan Apr 15 '20 at 11:26
  • 4
    Just map your site to wildcard like `server_name *.example.org;` in nginx. Then extract the domain from the request uri and check your database to see if it exists and then respond accordingly with a success or a 404 response. Add a wildcard dns cname entry in your domain's dns also. – Mat J Apr 15 '20 at 12:25
  • @MatJ: Thanks for your response. Do I need to make any changes apart from the ones you suggested in your response? For instance, do I need to create sub-directories or similar for each user inside my app. I have observed that folks do something to that effect using PHP, however, I am not sure if that is (creation of sub-folders for each user) a requirement for a Node js based app. Your inputs are highly appreciated. – Manvendra Singh Apr 15 '20 at 13:07
  • If you are trying to create a multi tenant application(muliple users with own settings accessing single app instance), you dont need to do that. If you want to provide seperate instance of your app to each client, then maybe.. that is not gonna be cost effective or scalable in any way though. – Mat J Apr 15 '20 at 14:10
  • @MatJ: Thank you very much. Do you mind taking a stab at a somewhat related question I posted here: https://stackoverflow.com/questions/61230367/how-do-i-provide-to-my-customers-the-ability-to-map-their-custom-domains-with-my? Much appreciated. – Manvendra Singh Apr 15 '20 at 14:20

0 Answers0