Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve custom domains pointing to a subdomain in Saas App

Tags:

dns

saas

hosting

I want to create an example SaaS app, whereby users are able to signup, create web pages, use templates and/or customize them with custom css, serve their web pages off custom domains.

I was considering saving the templates on S3/other CDNs, along with media/stylesheets/js files. While all are technically possible (practical? that could be debatable). Anyways, I was having a hard time figuring out how websites would be served off custom domains in this case? For instance, when they sign up, they could get a subdomain.domain.com address. However, how do they point customerdomain.com so that when customerdomain.com is entered, it serves same content as customerdomain.domain.com, and URL remains customerdomain.com

Additionally, if I want to have a "feature" whereby, custom domains could be a paid feature. How would I restrict it to paid users only?

Normally when, we setup websites, we specify it in virtual host config file (apache), and give it aliases, so it looks for and serves those aliases. In this case, I do not want to have a separate vhost file for each person who signs up. Is there an alternative? How can I program this? Are there any gotchas to be aware of?

One solution that I have seen is to have the server serve a wildcard domain i.e *.domain.com, and a separate vhost for each custom domain, however I would prefer to avoid if I can.

Thanks.

like image 460
Nasir Avatar asked May 03 '12 20:05

Nasir


People also ask

Does subdomain work with domain?

A subdomain is an add-on to your primary domain name. Essentially, a subdomain is a separate part of your website that operates under the same primary domain name. To create a subdomain, you must have a primary domain name. Without a primary domain name, there's no way to add a subdomain onto it.

Can domain and subdomain on different servers?

Yes, It's possible. For Example. You would either Contact your Host to set this, or if you have the ability to point DNS your self then do so. Just make sure that the site you want to serve on that subdomain exists on the server that it's pointing to.


1 Answers

The custom domain is usually done through a CNAME DNS record (sort of a symlink for DNS records). You tell your customer (who is usually in control of customerdomain.com) to create a CNAME record saying that customerdomain.com is an alias for customerdomain.domain.com. Then you need to set up your own server to interpret requests to customerdomain.com the same as it would treat requests to customerdomain.domain.com.

Depending on how you serve your subdomains, this can be done in a few different ways.

If you have a vhost file for every single customer, you need to add a "ServerAlias" directive for the custom domain your client has provided.

If you are coding the entry point through your own application server (say, reading the "Host" HTTP header from PHP and then setting the customer name from that) then you need to adjust that code accordingly to interpret requests for external domains according to your own database of custom domains. You can even use straight DNS for this!

Something on the lines of:

if http "host" header does not end in domain.com:
    cname = get_cname_record(http "host" header value)
    if cname does not end in domain.com:
        return error 404
    else:
        site = first part of cname
else:
    site = first part of http "host" header

Then you can use DNS as your "custom domain database". Make sure you are using a DNS cache though, as those queries will be performed on every single request.

like image 125
GomoX Avatar answered Sep 21 '22 07:09

GomoX