I got it working! this is the config file:
server {
listen 80;
server_name *.domain.com;
root /var/www/$host;
if (-e /var/www/config/ssl/$host) {
rewrite ^ https://$host$request_uri? permanent;
}
include error_pages;
include php_config;
}
I still had to use an if statement but that's alright because it works now.