This snippet works with Letsencrypt Wildcard certificates! Specify -d *.domain.com to install the certificate when using certbot.
Insert into server block. Change domain.com to correct domain.
server_name ~^(?<subdomain>.+)\.domain\.com$;
root /var/www/html/domain,com/$subdomain/htdocs;