Home > database >  NGINX how to set root dir depending on host
NGINX how to set root dir depending on host

Time:05-28

There is one nginx config for all websites on server.
All websites have one root directory except one that has another.
How to set root directory for that one host?

This config returns an error: "root" directive is not allowed here.

server {
    ...

    root "/webhome/$host/web";

    if ($host = site.example.com) {
        root /webhome/site.example.com/www;
    }
    
    ....

Also tried without success this:

location / {
    if ($host = site.example.com) {
        root /webhome/site.example.com/www;
    }
}

CodePudding user response:

While this is usually solved using several server blocks, you can use the map block to evaluate root directive parameter value:

map $host $siteroot {
    site.example.com  /webhome/site.example.com/www;
    default           /webhome/$host/web;
}
server {
    root $siteroot;
    ...
  • Related