1

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

2
  • 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, 2011 at 21:17
  • It's already inside in the Default Server Block. Oct 24, 2011 at 21:23

2 Answers 2

5

$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 https://www.nginx.com/resources/wiki/start/topics/tutorials/config_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;
  }
}
1

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.

4
  • Not really, it's the same in nginx 0.8 Oct 24, 2011 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. Oct 24, 2011 at 21:25
  • And where I should set this $document_root variable? Oct 24, 2011 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. Oct 24, 2011 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .