Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This is part of my nginx configuration file:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    include        fastcgi_params;
}

Almost the website loads because the location of / is also set to /var/www on this config file.

location / {
    root   /var/www;
    index  index.php index.html index.htm;
}

When I use phpinfo() and read PHP variables table, I noticed this:

_SERVER["DOCUMENT_ROOT"]    /usr/share/nginx/html

Other variables (most of them configured in nginx.conf)as server name, script filename, etc, were sent to php. So, nginx doesn't sending this variable to PHP. What I have to do?

I'm using PHP 5.3.8 and nginx 0.8

share|improve this question
    
Move your 'root' directive outside the location blocks - it should be in the server block (and verify that you have a line reading fastcgi_param DOCUMENT_ROOT $document_root; in your fastcgi_parms file) –  cyberx86 Oct 24 '11 at 21:17
    
It's already inside in the Default Server Block. –  Mr_LinDowsMac Oct 24 '11 at 21:23

2 Answers 2

up vote 2 down vote accepted

$document_root is set by the root directive. The 'root html;' line in the php location sets $document_root to <nginx prefix>/html. Take a look at http://wiki.nginx.org/Pitfalls#Root_inside_Location_Block to see how you should be setting your root in the server context. It should look something like this:

server {
  root /var/www;
  index index.php index.html index.htm;

  location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }
}
share|improve this answer

I'm using nginx 1.0.6, and this is in my fastcgi_params:

fastcgi_param  DOCUMENT_ROOT      $document_root;

Not sure if this will help you on 0.8, but updating nginx will help if not.

share|improve this answer
    
Not really, it's the same in nginx 0.8 –  Mr_LinDowsMac Oct 24 '11 at 21:22
    
@ianenri: It works perfectly on 1.0. Could be a bug in 0.8 then. Still, this is how you set it. You could set it manually rather than via the variable, that should solve your issue. –  Matthew Scharley Oct 24 '11 at 21:25
    
And where I should set this $document_root variable? –  Mr_LinDowsMac Oct 24 '11 at 21:40
    
@ianenri: I'd add a line to your location, directly after the include fastcgi_params that is something like fastcgi_param DOCUMENT_ROOT /var/www;. I doubt you can actually set $document_root. Also, look at kolbyjacks answer, as that's likely the root of the problem. –  Matthew Scharley Oct 24 '11 at 22:08

Your Answer

 
discard

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.