0

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

}

1 Answer 1

2

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;
    ...
}
2
  • this should manage access to any endpoint of my api? Ex: api.mydomain.com/endpoint
    – jssDev
    Commented Jun 20, 2022 at 8:12
  • 1
    root directive setting will be inherited by any location unless something else will be specified explicitly on the location configuration level. Commented Jun 20, 2022 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.