NGINX Dynamic subdomains

Create dynamic subdomains from folder with nginx.

just point to a folder for example /home/hugo/domains/ and then all folders in that folder will automaticly have a subdomain pointing to it.

Full config with php-fpm running.


server {
        listen 80;
    index index.php index.html index.htm;

    server_name ~^(?<subname>.*).dalahest.se$;

    root /home/hugo/domains/$subname;

    access_log /var/log/nginx/$subname.access.log;
    error_log /var/log/nginx/$subname.error.log;

    charset utf-8;

    location / {
            try_files $uri $uri/ =404;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/dalahest.se-0002/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dalahest.se-0002/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    sendfile off;

    client_max_body_size 100m;

    location ~ .php$ {
            fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME \"/home/hugo/domains/$subname$fastcgi_script_name\";
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /.ht {
            deny all;
    }

}