Nginx map subdomain to subfolder

In this tutorial I’ll show you how to direct your incoming web traffic dynamically to folders on your server. This is really useful for a lot of setups I use this one a lot for development environments because you can give developers rights to create new folders and it automatically has its own sub domain.

Example:

DNS endpoint Folder on disk
example.com /var/www/example.com/htdocs/
test.example.com /var/www/example.com/test/htdocs/
dev1.example.com /var/www/example.com/dev1/htdocs/

Nginx config

Place this in your server block and remove the current server_name

server_name     "~^(?<subdomain>.+).example.com$";
root            /var/www/example.com/$subdomain/htdocs;

That’s all, if you have questions or suggestions, post them bellow.

Cloud & Open-Source magician 🧙‍♂️

I try to find the KISS in complex systems and share it with the world.

comments powered by Disqus