Nginx – $ document_root variable cannot be used in the “root” instruction

I ran into this problem while setting up my nginx configuration. Does anyone know why this happens?

root /folder/my_root;

index index.php index.html index.htm;
error_page 404 /404 .html;
location = /404.html{
root $document_root/error_pages; //FAILS HERE with the error in the title
internal;
}

< /div>

This variable is set by the root instruction. You cannot use it in the root instruction itself because it will cause an infinite loop. < p>

See http://nginx.org/r/root

The path value can contain variables, except $document_root and $realpath_root.

Please use your own variables instead.

< /p>

set $my_root folder/my_root;
root /$my_root;
...

location = /404.html {
root / $my_root/error_pages;
)

And don’t try to change the leading slash into a variable. root $var will look for $var in some default directories, such as /usr/local/nginx or /etc/nginx.

I encountered this problem when setting up my nginx configuration. Does anyone know why this happens?

root /folder/my_root;

index index.php index.html index.htm;
error_page 404 /404 .html;
location = /404.html{
root $document_root/error_pages; //FAILS HERE with the error in the title
internal;
}

< /p>

This variable is set by the root instruction. You cannot use it in the root instruction itself, because it will cause an infinite loop.

See http: //nginx.org/r/root

The path value can contain variables, except $document_root and $realpath_root.

Please use your own variables instead.

set $my_root folder/my_root ;
root /$my_root;
...

location = /404.html {
root /$my_root/error_pages;
}

And don't try to change the leading slash into a variable. root $var will look for $var in some default directories, such as /usr/local/nginx or /etc/nginx.

Leave a Comment

Your email address will not be published.