Skip to content

August 2025 · SaaS · Laravel · Next.js · Architecture

The Power of Sub-Domain Architectures in SaaS

Why give each customer a sub-domain, what it takes to route them safely, and what can go wrong. Notes from building multi-tenant B2B products with Laravel and Next.js.

The Power of Sub-Domain Architectures in SaaS

Why a sub-domain at all

When a business buys your product, it wants to feel like theirs. clientA.example.com does that better than example.com/clientA. It also gives you a clean way to know who a request belongs to before any other code runs, and that single fact makes tenant isolation much easier to reason about.

Wildcard DNS and the first few lines of a request

A wildcard DNS record points every sub-domain at the same server, and Nginx passes them through. Then middleware reads the sub-domain, finds the tenant and sets the context for the rest of the request. If the tenant does not exist, stop right there with a clear response. Nothing after that point should ever have to ask who this customer is.

One database or many?

This is the big decision. A shared database with a tenant ID on every row is cheap and simple to run, but one missing filter leaks data. A database per tenant gives very strong isolation, but migrations and backups multiply. There is no universally right answer. I decide based on how sensitive the data is and how many customers I realistically expect.

Keeping Next.js aware of the tenant

With a separate front end, the tenant has to travel with every server-side request. I read the host in the Next.js server code and pass it to the Laravel API in a header. Caching needs extra care here, because a page cached for one tenant must never be served to another.

What I learned the hard way

Local development with sub-domains is annoying, so sort it out on day one. Test isolation directly: log in as tenant A and try to read tenant B’s records. And think about the boring things early, like what happens to a tenant’s data when they stop paying.

Md. Fawjul Azim

Focusing on building high-performance web applications with modern tech stacks.