I have a projects folder in which I put multiple folders, each having an html page.

So, the folder hierarchy looks something like this –


I want to have each of these folders as subdomains to my domain, so I had to create an nginx conf file for each one with a different root path.

I was looking for way to have this done automatically using Nginx and finally found it!

The idea was to have the root of nginx configurable dynamically.

The nginx conf file for this looks like this –

server {
    listen 80;
    listen [::]:80;

    # * Save subdomain to variable
    server_name ~^(?<subdomain>.+)\mydomain.com$;

    # * Check for a subdomain's directory existing
    if (!-d "/path-to-projects-folder/$subdomain") {
        return 404;

    # * Define root directory by the subdomain
    root "/path-to-projects-folder/$subdomain";
    location / {
        index index.html;

Reference: Resolving subdomains dynamically via Nginx

Now, every time, all I need to do is to add a subfolder to server some simple apps that I write for my own benefit!

Here are some of them –