Serving multiple websites from sub routes of a domain using Nginx

Few days earlier I was working on migrating the PyConf Hyderabad website from GitHub Pages to a DigitalOcean server in order to achieve archive support for the previous year and current website. The websites are static websites generated by Jekyll.


The objective was to serve various years of the conference websites in year wise sub routes. The domain was . So we wanted something like , . We had two separate repositories for both of the websites which we wanted to serve in the stated manner.


Nginx directives have an excellent way of accomplishing this by leveraging the power of regular expressions. Nginx is being widely used for most of the production level web servers. I was also using Nginx as the server in the DigitalOcean droplet.

Since the websites were in two different repos all I had to do is – whenever a request comes with a specific subroute I would point it to the specific directory path. This was done in the Nginx location directive. I had to change the Nginx config with the following similar code

location = / {
    rewrite "^.*$" /2019/ redirect;

location ~ ^/2017(.*)$ {
    alias /home/pyconf/hydpyconf2017/_site/$1;

location ~ ^/2019(.*)$ {
    alias /home/pyconf/hydpyconf2019/_site/$1;

Let us understand this step wise. There are two parts of this configuration

Step 1

Redirect home url to /2019/ domain . This is to redirect the root domain to the sub route of the present year website. The = modifier after the location directive matches the exact route which uses the rewrite directive to redirect it to the specified replacement expression.

Step 2

The ~ modifier is used for case sensitive regular expression matching. Here we specify that if we match a route starting with /2017 search that path in the directory specified by the alias directive. The $1 refers to the first matched group in the regex. We can use the groups in regex to form the replacement strings that we want to point to.

Thus you can see with the help of regular expressions we are able to serve two different static websites using Nginx directives.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s