Wildcard Subdomains for a Multi-Tenant SaaS: DNS, SSL and Routing
How to give every customer of your SaaS its own subdomain: wildcard DNS and SSL on Cloudflare, proxy routing, tenant lookup, cookies and custom domains.
Many SaaS products give each customer an address of its own: acme.yourapp.com, globex.yourapp.com and so on. Done right, a new customer can sign up and use their subdomain within seconds, without anyone touching DNS. I set up exactly this for a multi-tenant platform: wildcard DNS and SSL through Cloudflare, and separate frontend and backend services deployed through Coolify on one VPS. This guide covers the decisions and the details that usually cause trouble.
How a request finds the right customer
The flow has four steps. A wildcard DNS record sends every subdomain to your server. SSL covers every subdomain without issuing a certificate per customer. The reverse proxy on the server sends any subdomain to your frontend app. Finally, the application reads the host name, looks up which tenant owns that subdomain and shows only that tenant's data. The first three steps are set up once. The fourth is code, and it is where the real security work happens.
1. Wildcard DNS on Cloudflare
In Cloudflare DNS, add an A record with the name * pointing to your server's IP address, and keep it proxied (orange cloud) so traffic passes through Cloudflare's network. Records you create explicitly, such as www, api or mail, still take priority over the wildcard, so your marketing site, API and email keep working as before.
One side effect to plan for: every made-up subdomain now resolves to your server, including typos and names nobody has registered. The application must answer those with a proper 404 page, not silently fall back to a default tenant.
2. SSL for every subdomain
There are two connections to secure. Between the visitor and Cloudflare, Cloudflare's Universal SSL certificate covers the main domain and one level of subdomains (*.yourapp.com). It does not cover deeper levels such as eu.acme.yourapp.com, which need an advanced certificate, so keep tenant addresses to a single level.
Between Cloudflare and your server, use the Full (strict) SSL mode, so the connection is encrypted and the server's certificate is checked. The server needs a certificate that covers the wildcard: either a free Cloudflare Origin CA certificate for yourapp.com and *.yourapp.com, or a Let's Encrypt wildcard certificate. Let's Encrypt only issues wildcard certificates through a DNS challenge, so the proxy needs a Cloudflare API token limited to editing DNS for that one zone.
3. Routing on the server
On a Coolify server, the built-in reverse proxy decides which container receives each request. Give the API its own host, such as api.yourapp.com, and configure the frontend service to answer for any subdomain. Keeping the frontend and backend as separate services, as I did on that project, means each can be deployed and restarted on its own. My guide to deploying Next.js and an API with Coolify covers the server setup itself.
4. Finding the tenant in the application
Read the Host header, convert it to lower case, remove any port and check that it ends with your own base domain. The part before it is the tenant's subdomain. Look it up in a tenants table and cache the result briefly. If nothing matches, return a 404.
In a Next.js frontend, the usual approach is middleware that reads the host and internally rewrites the request to a route such as /[tenant]/dashboard. The visitor keeps seeing acme.yourapp.com/dashboard, while your pages receive the tenant as a normal route parameter and can load its name, logo and settings on the server.
5. Keep each customer's data separate
The subdomain only says which customer the visitor is trying to reach. The API must still prove that the logged-in user belongs to that customer. Put the tenant ID inside the login token, check it on every request, and never trust a tenant ID sent from the browser. If all customers share one database, every table that holds customer data needs a tenant_id column, every query must filter by it, and indexes should start with it so queries stay fast as data grows.
Test this deliberately before launch: log in as a user of one tenant and try that token against another tenant's subdomain and API calls. It should fail every time. A separate database per customer isolates data further but makes migrations and backups harder; for most early products, one database with careful filtering is the practical choice.
6. Cookies and CORS
Set session cookies without a Domain attribute. Such host-only cookies stay on acme.yourapp.com and are never sent to another tenant's subdomain. Setting Domain=yourapp.com shares the cookie with every subdomain, which you should only do deliberately, for example for a central login, and with the tenant check above in place.
Because the frontend runs on many subdomains and the API runs on one, the API's CORS settings must allow those origins. Check each origin against the same subdomain rules your app uses instead of allowing every origin.
7. Signing up a new tenant
With a wildcard in place, signing up a customer is a database insert, not a DNS change. Validate the requested subdomain: lower-case letters, digits and hyphens only, no hyphen at the start or end, and no more than 63 characters, which is the DNS limit for one label. Keep a list of reserved names your own services use or may need later, such as www, api, admin, app, mail, status and docs.
8. Custom domains, when customers ask
Larger customers often want their own address, such as portal.theircompany.com. They point a CNAME record at your service, and you then need a certificate for a domain you do not control. Cloudflare for SaaS (custom hostnames) handles certificate issuing and renewal for this. Store custom domains in their own table that maps each hostname to a tenant, verify ownership before activating one, and use the same host lookup for both subdomains and custom domains.
9. SEO and the marketing site
Keep the public marketing site and blog on the main domain or www, where they build ranking over time. Tenant dashboards sit behind a login and should not be indexed, so send a noindex header on them. If tenants have public pages, remember that search engines treat each subdomain as its own site, so each page needs a canonical URL on its own subdomain. The Next.js SEO checklist covers the marketing side.
A launch checklist
Before inviting real customers, confirm that: the wildcard DNS record is proxied and explicit records still work; SSL is Full (strict) with a wildcard certificate on the server; unknown subdomains return a 404; the tenant comes from the host name and is checked against the login token; every query filters by tenant; one tenant's token is rejected on another tenant; session cookies are host-only; CORS checks origins against your rules; subdomain names are validated and reserved names are blocked; and the setup is documented so your team can maintain it.
Planning a multi-tenant product or moving an existing one onto its own server? See my deployment and server setup service, or the Next.js and Node.js API services if you need the application built too. Send a short brief and I will reply with a plan.
Case studies using this approach
Hire me for this
Deployment & DevOps
A finished app that isn't live earns nothing. I set up servers, containers, domains and SSL so your product runs on…
Learn more →ServiceNext.js & React development
I build fast, SEO-friendly web applications with Next.js, React and TypeScript: SaaS products, client portals, admin dashboards…
Learn more →ServiceNode.js, PHP & API development
The backend is where business rules live. I design MySQL schemas and build REST APIs in Node.js/Express or PHP that web and…
Learn more →