Multi-Tenant Custom Domains (Cloudflare for SaaS)
Each tenant can bring their own domain. apiportal.acme.com resolves to the Acme tenant without anyone on the Lockstep team ever touching a Railway dashboard. This doc is the runbook.
The big picture
Customer │ │ https://apiportal.acme.com ▼Cloudflare edge (Cloudflare for SaaS) │ — terminates TLS with a per-hostname cert │ — passes the request to the fallback origin ▼cname.lockstepgroup.com → Railway edge │ ▼portal Railway service │ │ Host: apiportal.acme.com ▼tenantMiddleware reads Host → tenant_domains.hostname lookup → tenantId │ ▼All DB queries scoped to Acme's tenantIdOne-time Cloudflare setup (Lockstep admin, ~15 min)
- Add
lockstepgroup.comto Cloudflare (already done if it’s in your Cloudflare account). - Create the CNAME target — in your Cloudflare DNS for
lockstepgroup.com:- Type:
CNAME - Name:
cname(full:cname.lockstepgroup.com) - Target: your Railway portal URL (e.g.
portal-production-44e1.up.railway.app) - Proxy: orange cloud on
- Type:
- Enable Cloudflare for SaaS — Dashboard → Websites → lockstepgroup.com → SSL/TLS → Custom Hostnames
- Set the fallback origin — when Cloudflare for SaaS asks for a fallback origin, enter
cname.lockstepgroup.com - Create the fallback origin CNAME record (Cloudflare will tell you the exact name — usually
_fallback.lockstepgroup.com) pointing back at Railway - Choose validation method — select TXT (HTTP works too, but TXT matches our in-app flow)
- Set
PLATFORM_CNAME_TARGETin Railway env vars on theportalservice tocname.lockstepgroup.com— the Platform config module picks this up everywhere admins see “CNAME your domain to…”
Once that’s done, Cloudflare is ready to accept new custom hostnames programmatically.
Adding a customer’s domain (superadmin, ~5 min + customer DNS work)
In the portal:
- Navigate to
/tenants(superadmin only) - Find the tenant → click + Domain
- Enter
apiportal.acme.com→ click Issue TXT - Share the TXT record (name + value) with the customer
The customer:
- Adds the TXT record at
_ltg-verify.apiportal.acme.comwith the value you shared - Tells you when it’s done
Back in the portal:
- Click Verify next to the pending domain
- On success, the portal shows: “Now CNAME apiportal.acme.com → cname.lockstepgroup.com”
The customer:
- Creates a CNAME at
apiportal.acme.com → cname.lockstepgroup.com - Propagation: usually 1-5 min
In Cloudflare dashboard (automatable — see § Automation below):
- SSL/TLS → Custom Hostnames → Add Custom Hostname →
apiportal.acme.com - Cloudflare will issue a per-hostname cert automatically
Once the cert is live, traffic to apiportal.acme.com reaches Railway, the portal’s tenantMiddleware matches the hostname against tenant_domains, and all requests are scoped to Acme’s tenantId.
Sync Authentik redirect URIs to the new domain
The tenant’s Authentik OAuth application (created via /integrations/applications earlier) needs the new hostname added as a valid redirect URI so OAuth sign-in works from the new domain.
Automatic (when you promote a domain to primary — not implemented in Phase A; manual step today):
# Fetch the tenant + its verified domainscurl -s $PORTAL/api/tenants/$TENANT_ID | jq '.authentikProviderPk'
# PATCH the Authentik OAuth2 provider's redirect_uris# (calls /api/v3/providers/oauth2/<pk>/ on Authentik directly)Manual (until the automation ships):
- Superadmin →
/integrations/applications→ find the tenant’s app - Edit it → add the new redirect URI:
https://apiportal.acme.com/api/auth/callback/authentik
Automation
Phase B will add a POST /api/tenants/:id/domains/:domainId/promote endpoint that:
- Marks the domain as
isPrimary - Calls Cloudflare API to add the custom hostname (requires
CF_API_TOKENenv var with Zone:SSL:Edit scope) - Calls Authentik API (
PATCH /api/v3/providers/oauth2/:pk/) to append the redirect URI - Returns the cert-provisioning status so the UI can poll
Until then, steps 1-3 are manual dashboard actions.
Testing a new tenant’s domain
# 1. Confirm DNS points to Cloudflaredig apiportal.acme.com# → expects CNAME apiportal.acme.com → cname.lockstepgroup.com → Railway
# 2. Confirm TLScurl -vI https://apiportal.acme.com/health 2>&1 | grep "SSL certificate"
# 3. Confirm tenant resolutioncurl -s https://apiportal.acme.com/api/me/tenant# → should return Acme's tenant recordIf (3) returns the Lockstep (default) tenant, the hostname isn’t in tenant_domains — check the portal’s /tenants page to confirm it’s verified.
Costs
- Cloudflare for SaaS is free up to 100 custom hostnames per zone.
- Beyond 100, Cloudflare Enterprise pricing applies.
- Railway charges per service, not per custom domain — adding tenants doesn’t increase the Railway bill.
Rolling back a tenant’s domain
If a customer offboards:
- Portal → Tenants → the tenant → find the domain → Delete
- Cloudflare dashboard → SSL/TLS → Custom Hostnames → remove
apiportal.acme.com - The customer removes their CNAME (optional — once Cloudflare drops the hostname, traffic 404s anyway)
Failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
apiportal.acme.com resolves to default tenant |
Domain not in tenant_domains or not verified |
Re-verify TXT |
| Browser shows generic Cloudflare error | Custom hostname not added in Cloudflare dash | Add it in SSL/TLS → Custom Hostnames |
OAuth callback fails with redirect_uri mismatch |
Redirect URI not in Authentik app | Add via /integrations/applications |
| Cert pending for >10 min | Cloudflare DCV failed (customer’s CNAME is wrong) | Re-check dig apiportal.acme.com |
| Cache lag after verification | Middleware’s 60-second LRU cache | Wait or restart portal |
Related
- Multi-Tenant Architecture (Phase A design doc)
- Applications (Authentik)
- Platform Config