Software development · SaaS · Server management · Tutorial

Custom domains for multi-tenant SaaS with automatic HTTPS

How to let every customer use their own domain on your SaaS, with certificates issued automatically by Caddy's on-demand TLS and a safe ask endpoint.

To give SaaS customers their own domains, have them point a CNAME at a hostname you control, put Caddy in front of your app with on-demand TLS so a certificate is issued the first time each domain is visited, protect it with an "ask" endpoint that approves only domains in your database, and resolve the tenant in your app from the Host header.

GoFloe lets every agency run its workspace under its own domain, so their clients see portal.their-agency.com rather than our brand. Custom domains sound like a small feature until you have to issue and renew HTTPS certificates for thousands of domains you don't own. Here is the setup that makes it nearly hands-off.

How the pieces fit together

  1. The customer adds their domain in your app's settings. You store it as pending.
  2. They create a DNS record: portal.their-agency.com CNAME domains.yourapp.com.
  3. The first HTTPS request arrives at your server for a domain it has no certificate for.
  4. Caddy asks your app whether that domain is allowed. If yes, it gets a certificate from Let's Encrypt or ZeroSSL in a few seconds and serves the request.
  5. Your app reads the Host header and loads the right tenant.

The Caddyfile

{
	on_demand_tls {
		ask http://localhost:3000/internal/domain-check
	}
}

# Your own domains, configured normally
app.yourapp.com {
	reverse_proxy localhost:3000
}

# Every other hostname: customer domains
https:// {
	tls {
		on_demand
	}
	reverse_proxy localhost:3000
}

Renewals are automatic. Caddy stores certificates in its data directory, so keep that on persistent storage. If you run several Caddy servers behind a load balancer, give them shared storage so they don't each request their own certificates.

The ask endpoint is not optional

Without it, anyone could point thousands of random domains at your server and make it request certificates for all of them, hitting the certificate authority's rate limits and blocking real customers. Caddy calls your endpoint with ?domain= and only proceeds on a 200 response:

app.get('/internal/domain-check', async (req, res) => {
  const domain = String(req.query.domain || '').toLowerCase();
  const tenant = await db.tenants.findByCustomDomain(domain);
  if (tenant && tenant.domainStatus !== 'disabled') return res.sendStatus(200);
  res.sendStatus(404);
});

Keep it fast and make sure it is only reachable from the server itself.

Resolving the tenant

In your app, look up the tenant from the Host header on every request, with a cache in front of the database lookup. Two details that save debugging time later:

  • Normalise the host: lowercase it and strip the port.
  • Make sure your framework trusts the proxy, so generated URLs, redirects and secure cookies use the customer's domain and https.

Verify the DNS before you go live

In your settings screen, check the record yourself before marking a domain active. Resolve the CNAME from your server and confirm it points at your target. Show the customer exactly what to fix if it doesn't. Most support tickets about custom domains are DNS records with a typo or a proxy setting at their DNS provider.

Root domains like their-agency.com can't have a CNAME at the apex. Either ask customers to use a subdomain, or offer a stable IP address for an A record (or support providers' CNAME-flattening and ALIAS records).

Small things that make it feel polished

  • Show a live status: waiting for DNS, issuing certificate, active.
  • Redirect the tenant's default subdomain to their custom domain once it's active, so links and SEO consolidate.
  • Email the customer if their DNS later stops pointing at you, before certificate renewal fails.

FAQ

How long does it take to issue a certificate for a new custom domain?

Usually a few seconds on the first visit once DNS points at your server. The first request waits for issuance; every request after that is served normally.

Do I need a wildcard certificate for custom domains?

No. Wildcards only cover subdomains of a domain you own. Customer domains each need their own certificate, which on-demand TLS issues automatically.

Can customers use a root domain instead of a subdomain?

Yes, but not with a plain CNAME. They need an A record pointing at a stable IP address, or a DNS provider that supports ALIAS or CNAME flattening at the root.

Keep reading