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;
    }

}

asked Jun 20, 2022 at 6:53

jssDev's user avatar

jssDevjssDev

9939 silver badges21 bronze badges

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;
    ...
}

answered Jun 20, 2022 at 7:54

Ivan Shatsky's user avatar

Ivan ShatskyIvan Shatsky

15.8k2 gold badges25 silver badges51 bronze badges

2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.