Questions about multi-tenant setup

Hi! I am considering moving from Vercel to Netlify because it looks like you have better support for multi-tenant apps. Reading other threads on wildcard subdomains, I know that it needs to be a Pro+ team account.

Appreciate it if you could help with my questions before migration.

Question 1:
Assuming my primary domain is site.dev. I can only use *.site.dev for wildcard subdomains and it is not possible to have *.users.site.dev. Is that correct?

Question2:
I already have some other subdomains in use. They are set up using CNAME records. For example auth.site.dev has a CNAME to my Firebase auth, or 12242.site.dev has a CNAME to sendgrid.
My understanding is that I need to add either a CNAME with * to my Netlify site address or I can add CNAMEs for subdomains individually (like a CNAME for steve.site.dev and another CNAME for ben.site.dev both pointing to my Netlify site address.).

To be clear let me ask my question with an example. Assuming there are these 2 DNS records on my domain (site.dev). They are used to set wildcard subdomains, and also to set custom auth service:

  • CNAME: * → mysite.netlify.app
  • CNAME: auth → custom.auth.provider

Am I correct to assume that if you go to https://auth.site.dev, the request will go to custom.auth.provider? I am assuming that the specific value (auth in this case) will take precedence on the wildcard (*).

Question 3:
As for the multi-tenant app. The app is build using Nuxtjs. Each user gets a path (/profile/<username>). After setting up the wildcard subdomains, I want to rewrite https://<username>.site.dev/* with https://site.dev/profile/<username>/*. Is that something that can be achieved using netlify.toml or you suggest an edge function for it?

Thanks a lot for your time.

Technically, you’d need to have the domain www.site.dev or basically <anything>.site.dev for *.site.dev to work on that site. But yes, you can’t have *.users.site.dev on the same site, you can do that on a different one.

Yes, and that has nothing to do with Netlify, but that’s just have DNS will work. Specific entries will take preference over wildcards.

You’d have to use Edge Functions or even a middleware in Nuxt would work. Nuxt can be deployed to Edge Functions, so no need to write a separate Edge Function just to handle this redirect, unless you need Nuxt to be deployed to AWS Lambda due to whatever reason.

Thank you @hrishikesh for taking the time and for your helpful answer :slight_smile:

On the 3rd question, my requirement is to rewrite (not redirect) <username>.site.dev to www.site.dev/profile/<username>, to be clear, when a user goes to john.side.dev/* the URL won’t change but the served response will the same as www.site.dev/profile/john/*. That way each user can set a CNAME on their domain pointing to our subdomain in order to have their profile and subpaths under their domain.

For example: when you go to johndoe.com, while your browser url is showing https://johndoe.com you will see the contents that is also available at https://www.site.dev/profile/john/.

Hope it makes sense and looks like it is possible to achieve with Netlify.

What I appreciate help with is understanding what the Edge Function workflow would look like.

I understand that a rewrite is possible. And I also understand that rewrite is the same as a redirect but with status code 200. Is there an example of how to setup with rewrite from subdomain to path using edge functions?

Looks like my question is very similar to this one, I hope things have changed since 2021.

Untested, but something like this:

export default async (req : Request) => {
  const reqUrl = new URL(req.url)
  return new URL(`/profile/${reqUrl.hostname.split('.')[0]}/${reqUrl.pathname}`, 'https://www.site.dev/')
}

should work for a URL like: https://john.com/ that needs to be rewritten to https://www.site.dev/profile/john/. This would further take into account the path on the original URL, for example: https://john.com/foo/ would now be rewrriten to https://www.site.dev/profile/john/foo/. This would not work as intended for https://www.john.com due to the www in the hostname. It would require some special handling.

1 Like

That’s a great response @hrishikesh. I know have a better understanding.

Now my next quest is to find out how to return a URL as the response using a Nitro middleware (Nuxt server middleware). My idea is to have a Nuxt server middleware (~/server/middleware/subdomain.ts). This middleware will be executed whenever a request hits any server route. In side I can do the checks and send the new URL.

The thing is I don’t know how to do that at the moment.

Ah! scratch that!

Nuxt server middlewares are not supposed to return a response.

Middleware handlers should not return anything (nor close or respond to the request) and only inspect or extend the request context or throw an error.

So, that’s not possible with a Nuxt middleware to check if the request is coming from a subdomain and rewrite it to a path.

Using a Netlify Edge function looks pretty straightforward though. I think I just need to add a function that runs on every request to server, checks the request host, and if it is a subdomain, rewrite it to a path, by returning a new URL. Is that right?

I don’t know how to do that in Nuxt, though.

Yeah it appers that Nuxt doesn’t really support rewrites. You would have to rely on native Netlify Edge Functions then.