Home > Net >  Nginx dynamic root from subdomain
Nginx dynamic root from subdomain

Time:06-22

My website uses many subdomains. What I need is to root requests to each folder depending of subdomain:

  • src.mydomain.com to /public
  • api.mydomain.com to /public
  • Anyother subdomain xxx.mydomain.com to /dist

I tried this settings without success:

server {
    listen 8080;
    listen [::]:8080;
    
    server_name ~^(?<subdomain>. )\.mydomain\.com$;
    
    set $folder "dist";
    if ($subdomain = "src"){
    set $folder "public";
    }
    if ($subdomain = "api"){
    set $folder "public";
    }
    
    root "/home/site/wwwroot/$folder";
    index  index.php index.html;

    location / {            
        index index.php index.html;
    }

}

CodePudding user response:

Try this:

map $http_host $webroot {
    src.mydomain.com  /home/site/wwwroot/public;
    api.mydomain.com  /home/site/wwwroot/public;
    default           /home/site/wwwroot/dist;
}
server {
    server_name *.mydomain.com;
    root $webroot;
    ...
}
  • Related