Setup wildcard domain routing using Laravel Forge

Posted 18th Jan 2019

I'm leaving this blog post here as a bit of a reminder to myself, as I know I'll come across this again some time in the future.

This particular problem has stumped me once before, and I never managed to solve it back then. However, I've now figured out how to point multiple dynamic domains to the same site on Laravel Forge.

This is useful if you need to build a Laravel application that can be run on multiple domains, but you're not sure what those domain names will be yet. For example, you might have a SAAS application that allows users to setup custom domain names on their profiles.

It's recommended that you use this technique on a new server with only this site running, as you'll be fighting with other nginx setups if there's more than one site.

Create a new site on Laravel Forge

First, setup a new site on your server and name it anything you'd like - it doesn't have to be an actual domain name.

Once your site has been created, click on it and edit the nginx file (using the dropdown in the bottom right).

undefined

The only thing you need to amend, is where it says 'server_name WEBSITE' (or whatever you named your site).


Simply change this to say either...

server_name ~.;

Or if you wish to allow wildcard subdomains also...

server_name .~.;

That's it, simply point what every domains you want to your server IP, and this one site config will take care of the rest.

Next steps...

If you're looking for a reference on how to setup your dynamic domain routes within your Laravel app, Joe Lennon has a great post on how he overcame this here.