Subdomains with Linode and Nginx

May 20, 2013

This post will show how I created subdomains on my Linode box with Nginx. Although I’m using Linode and Nginx as a reference, I assume this could be generalized with other types of domains and web servers. Take a look at one my subdomains at https://doc1.minh.io. Listed below are the steps I took to get that to work:

1. Log into Linode and add an A/AAAA record

Hostname IP Address TTL
dev 10.10.10.10 Default

One issue I encountered was that my Domain Zone was “www.minh.io” when I needed it to be “minh.io” so I cloned “www.minh.io” to “minh.io” and deleted “www.minh.io.” This solved an issue where I couldn’t access “minh.io” and also my subdomain was “dev.www.minh.io” when it should’ve just been “dev.minh.io”

Also keep in mind that it takes a couple of minutes for the subdomain DNS changes to propagate across the internet. I initially had some trouble figuring this out and kept trying new changes until eventualy I saw the subdomain.

2. Update the Nginx server configuration

It depends on how your nginx is set up, but typically Nginx is installed to /etc/nginx/ and the Nginx configuration in /etc/nginx/nginx.conf usually has ‘include /etc/nginx/sites-enabled/*;’

So, if you have server configuration like “mysite.io” in /etc/nginx/sites-enabled then you could modify it like the following example:

server {    
        listen          80;
        server_name     mysite.io;
        location / {
            root   /srv/www/mysite.io/public_html/static;
        }
}

server {
        listen          80;
        server_name     sub.mysite.io;
        location / {
            root   /srv/www/mysite.io/public_html/static;
        }
}

Lastly, don’t forget to restart Nginx (service nginx restart) and you should see the subdomain after ~30 minutes.