Automatic subdomains in Nginx

In Nginx, there is a quick way of automatically creating subdomains with the following config.

server {
 listen 80;
 server_name "~^(?<sub>.+)\.domain\.tld$";
 root /home/public/subdomains/$sub;
}

Doing this, calling sub1.domain.tld will serve content from the folder at /home/public/subdomains/sub1.

This is not an proper production sample, as you would also need to set things like index, query and php in addition to some added security.

bb@morningtrain.dk'

Bjarne Bonde