Search code examples
nginxhostnamecname

Nginx conf for a SaaS - is there a decent way to handle this scenario?


I'm developing a web app. Users will sign up and will be assigned a sub-domain which will allow them to access content via account-456.myappsdomain.com. The intention of having that sub-domain though isn't for them to visit it directly. Instead, they'll be informed to point the CNAME for www.their-own-site.com to account-456.myappsdomain.com in order for them to be able to visit their own domain and view their content directly on their own domain. Is is possible, using Nginx, to create a configuration that will allow this without me having to edit the config or add a virtual host every time a user signs up for an account?

Currently, my app uses the host header (splitting on the dots to get the account-456 bit) to get the account ID, and then sets up the theme based on that. I assume that with this new sort of configuration, I'd check the host header for their domain (which I will have had them enter into the app), query for the domain (and the account it belongs to) and set up their theme that way. Is this correct?


Solution

  • To answer your question fine sir, a site configuration for wild card sub-domains can be made in Nginx, and a CNAME for www.example.com pointing to a acct-123.yourappsdomain.com will cause visits to the former to be treated like the latter. It's up to you to listen for the hostname of www.example.com, looking up in the database the account info for the domain, and loading the correct content. Basically, you'd use the same logic you use to pull up the sub-domain for CNAME domain.