Managing Wildcard Subdomains in Laravel Homestead on Ubuntu

04 March 2017

Imagine the scenario where you have multiple subdomains, such as multiple locales for the internalization of your application served as en.example.test and es.example.test. You don't need to put each of them as a seperate entry to your both homestead config file and the hosts file if all of them will be served from the same application. There is of course an easier way.

Homestead Configuration

Let's assume our application is located within the ~/Code/Example folder and we want to map it to example.test, furthermore, the ip address assigned to our box is 192.168.10.10

$ nano ~/Homestead/Homestead.yaml

The most simple Homestead config will be like below.

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub

keys:
- ~/.ssh/id_rsa

folders:
- map: ~/Code/Example
to: /home/vagrant/Code/Example

sites:
- map: example.test
to: /home/vagrant/Code/Example/public

databases:
- homestead

Nginx Configuration

Then, ssh into your vagrant box and edit the nginx.conf of the example.test.

$ cd ~/Homestead && vagrant up && vagrant ssh
$ sudo nano /etc/nginx/sites-available/example.test

And just add the wildcard entry to the server_name.

server_name example.test *.example.test

And reload the nginx configuration.

$ sudo service nginx reload
$ exit

dnsmasq Entry

Then on your local machine, add a dnsmasq entry.

$ sudo nano /etc/NetworkManager/dnsmasq.d/local

Just add the entry below.

address=/example.test/192.168.10.10

Finally, restart the network-manager.

$ sudo service network-manager restart

Now everything should be ok. You don't need to add individual entries for each of the subdomains of your application. For instance, both en.example.test and es.example.test will be served without adding a seperate configuration for each.