
I have a projects folder in which I put multiple folders, each having an html page.
So, the folder hierarchy looks something like this –
projects |-sampleproject1 |-index.html |-style.css |-sampleproject2 |-index.html |-style.css
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 serve some simple apps!
Here are some of them –