Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NGINX how to set root dir depending on host

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading