Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The $document_root variable cannot be used in the "root" directive

Tags:

nginx

While setting up my nginx configuration I came upon this. Does anyone have any idea on why this happens exactly?

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;
 }
like image 318
Fane Avatar asked Dec 24 '22 11:12

Fane


1 Answers

This variable is set by root directive. You cannot use it in root directive itself, because it will lead to infinite loop.

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

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

Use your own variable instead.

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

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

And don't try to put leading slash into variable. root $var will look for $var in some default directory like /usr/local/nginx or /etc/nginx.

like image 75
Alexey Ten Avatar answered May 18 '23 16:05

Alexey Ten