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:
-
Update the
server_namedirective: Add a wildcard to match any custom domain. - 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:
# 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 *.initech.com; # Add wildcard for custom domains
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;
}
}
# New server block to handle custom domains
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.initech.com; # Match custom domains
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;
# Redirect custom domains to the appropriate subdomain
location / {
set $subdomain "";
if ($host ~* ^(.*)\.initech\.com$) {
set $subdomain $1;
}
proxy_set_header Host $subdomain.example.com;
proxy_pass http://127.0.0.1:80;
}
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/*;
In this configuration:
- The first server block handles requests for
example.comand any subdomains ofexample.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 theHostheader to the corresponding subdomain ofexample.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.