Build a vagrant development environment that will serve an rails application that works on multiple subdomains. Moreover that application consists of multiple smaller apps that all are running with a single domain.
example.dev–> Application #1
app.example.dev–> Application #2
<any other subdomain>.example.dev–> Application #3
Let’s assume we’re using
.dev domains, because we’re used to pow. Meaning those routing should work within vagrant machine and same subdomains should work exactly same way on our local machine.
Solution – nginx & dnsmasq
(I assume you’re running some kind of ubuntu-based distro, paths may vary between distros)
Install nginx & dnsmasq. Edit file
/etc/dnsmasq.d/dev-tld as follows:
dnsmasq service. This rule will forward traffic from .dev domains to your localhost – and this is exactly what we want.
Now let’s configure nginx, following the scenario above we will need configuration for three apps. We will need some kind of server (thin, puma, unicorn, whatever) that will listen on unix socket. In our nginx config / sites-enabled we need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Nging will listen on port 80 and forward request according to our needs. Traffic from
example.dev will be served by our first app (app1),
app.example.dev will be served by app2 and any other traffic (
*.example.dev) will go through app3.
So this works, nice & easy. We have 3 rack servers listing on UNIX sockets backed by nginx that routes everything. From vagrant box you can access all
.dev domains, so far – very cool.
Next step – accessing .dev subdomains from local machine
It’s nice, but it’s not very useful at this point really – we have no way for accessing
example.dev (or any of it’s subdomains) from our local machine. But there is already solution for that – some smart folks already created tools for us that solves this problem.
First we need to route traffic from ninx in vagrant (port 80) to our machine, in Vagrantfile you need:
1 2 3
Note: don’t forget to restart your vagrant vm after making changes to Vagrantfile.
Now, depending on what system you are using:
- on Mac you have already mentioned pow, after installation you go to
- on Linux you have prax, very similarly – after installation you to to
~/.praxand type the same thing
pow/prax will now forward traffic from
*.example.dev on your local machine to port 8080 – so basically this will be handled by nginx in vagrant and it just works with our subdomains setup. Magic.