Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my nginx server {} block I have root defined with a path. How can I later reference this path?

This is what I'm trying to accomplish:

server {
    listen 80;
    root /var/www/foosite;


    include $root/*.ngaccess;

Unfortunately using $root doesn't work (because I haven't defined it). Does Nginx provide a way to access the root value?

share|improve this question
up vote 9 down vote accepted

You want the document_root variable.

share|improve this answer
Doing this: root /var/www/foosite/public/; include $document_root.ngaccess; I get the error "nginx: [emerg] open() "/etc/nginx/$document_root/.ngaccess" failed (2: No such file or directory)." $document_root seems like it would be the solution, but have I typed something wrong? According to this post (and others I came across), variables cannot be used in include statements. Which, if true, is unfortunate. – skyler May 14 '12 at 2:57
That implies that nginx is only evaluating the include directive at startup, not for every request (which makes sense), so it seems that what you want to achieve is not possible unfortunately. – mgorven May 14 '12 at 6:47
I ran into a problem here too: "The path value can contain variables, except $document_root and $realpath_root."… – editor Nov 4 '15 at 19:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.