Search code examples
nginxroot

Change nginx root directory dynamically


How to change nginx root directory to another directory dynamically without a redirect, depending on a certain condition? For instance, if:

$http_user_agent ~* "googlebot"


Solution

  • something like this

    server {
        ...
    
        if ($allow = no) {
            set $variable "folder_1";
        }
        if ($allow = yes) {
            set $variable "folder_2";
        }
        root  /var/www/$variable;
    
        ...
    
        location / {
          ...
        }
    }