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:
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/example.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com *.example.com
server_tokens off;
root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/12345/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/12345/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128...;
ssl_prefer_server_ciphers off;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/example.com/after/*;
I’m I on right path? Can I modify the Nginx config above to accept traffic from “initech.com” and handle it as expected?