Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cameronscott137's avatar

Adding a CNAME record to a customer subdomain

My Laravel app allows customers to create their own subdomains using the framework's built-in subdomain routing:

Route::domain('{domain}.example.com')->group(function () {
    Route::get('/user/{id}', function (string $account, string $id) {
        // ...
    });
});

I would also like allow my customers to add CNAMEs to use their own custom domains, e.g. "initech.com" has a CNAME record pointed to "initech.example.com."

However, I've added a test CNAME record, but when I visit "initech.com," I'm served the main page of "example.com," and not the "initech.example.com" page that I was expecting.

I think that this is a server configuration issue, and that I need to make an update to my Nginx configuration to tell it how to properly serve content from "initech.com."

My current Nginx configuration is the default created by Laravel Forge:

I'm I on right path? Can I modify the Nginx config above to accept traffic from "initech.com" and handle it as expected?

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, you are on the right path. To handle custom domains like "initech.com" and route them to the appropriate subdomain in your Laravel application, you need to update your Nginx configuration to recognize these custom domains and direct them to the correct location.

Here's how you can modify your Nginx configuration to achieve this:

  1. Update the server_name directive: Add a wildcard to match any custom domain.
  2. Add a new server block: Create a new server block to handle the custom domains and redirect them to the appropriate subdomain.

Here's an updated version of your Nginx configuration:

In this configuration:

  • The first server block handles requests for example.com and any subdomains of example.com.
  • The second server block handles requests for custom domains like *.initech.com.
  • The location / block in the second server block extracts the subdomain part of the custom domain and sets the Host header to the corresponding subdomain of example.com.

Make sure to reload or restart Nginx after making these changes:

sudo nginx -s reload

This should allow your custom domains to be properly routed to the corresponding subdomains in your Laravel application.

Please or to participate in this conversation.