1 week ago

Laravel Custom Domain : DNS, Nginx, and Routing

Posted 1 week ago by Darren_Ter

I am currently working for a CRM with domain name ( Exp: abc.com ). Each registered user will be assigned a subdomain ( Exp: 123.abc.com ), for them to design a landing page, and publish their landing page using that subdomain name.

The subdomain is worked well now. But now I want to step one level further, which each user can buy their own domain name ( Exp: apple.com ), and point their domain name to our server, and serve the exact same content as 123.abc.com.

After doing some research online, this is what I have now.

In my web.php :

Route::group(['domain' => '{customDomain}'], function () {
        return $customDomain;

In my RouteServiceProvider :

To allow non-alphanumeric characters in custom domain :

public function boot()
    \Route::pattern('domain', '[a-z0-9.\-]+');

In my Nginx web server ( /etc/nginx/sites-available/abc.com ) :

To accept all incoming requests to my server :

server_name ~.;

That's all is what I done in Laravel.

In user's DNS provider ( Cloudflare ) :

I ask them to point their custom domain name to our website :

CNAME      apple.com       abc.com       DNS only 

Although all the things have been set up, but once the user type apple.com, it shows up this error :

enter image description here

Even the user have change the dns record to point to our server instead :

A      apple.com         DNS only

It does successfully show up our website, but my route for custom domain doesn't work. It show up the login page instead of return $customDomain.

Here I have a few questions :

  1. Do I need to ask the user to point to our website using CNAME record? But how to solve the cnamecross-user banned ?
  2. Is it correct to set my Nginx web server to accept all request like this : server_name ~.;
  3. Is there any errors on my custom domain route? I place it at the very top of route list.
  4. Is this feature consider a multi-tenancy system? We do ask a senior before, he said we need to almost rebuild our website for this feature, because it will change the infrastructure of our website. But we don't design to be each user will have their own database, all users will share a database.
  5. Do we need to do anything in cloudflare for such feature? Currently, we have the wildcard dns record ( A * DNS only ) that accepts all requests already.

Please sign in or create an account to participate in this conversation.