Trailing slash being removed from document root

Hi all. We’re considering switching from lighttpd to nginx in my
company and so far everything looks good except for one thing I can’t
seem to figure out.

When getting the document root in PHP from nginx using this:

$_SERVER[‘DOCUMENT_ROOT’].“…/”.$ZONE.“/”.$LOCALE…etc…

nginx will always return the path WITHOUT the trailing slash. I have
the document root configured like this:

root /home/web/www.site.com/html/; # note the trailing slash

We have way too much code in php that gets the document root without
adding a trailing slash after $_SERVER[‘DOCUMENT_ROOT’] because lighttpd
and apache before that always had preserved the trailing slash.

How can I configure nginx to keep the slash?

Thanks!

Posted at Nginx Forum:

On Wed, Jan 04, 2012 at 05:30:42PM -0500, freejack wrote:

Hi there,

When getting the document root in PHP from nginx using this:

$_SERVER[‘DOCUMENT_ROOT’]."…/".$ZONE."/".$LOCALE…etc…

nginx will always return the path WITHOUT the trailing slash.

nginx doesn’t do PHP, it does fastcgi. Which means that it sends a
series
of key/value params to the fastcgi server, which in turn presents them
to your PHP in the _SERVER array.

Which in turn means that…

We have way too much code in php that gets the document root without
adding a trailing slash after $_SERVER[‘DOCUMENT_ROOT’] because lighttpd
and apache before that always had preserved the trailing slash.

How can I configure nginx to keep the slash?

…wherever you currently set “fastcgi_param DOCUMENT_ROOT” (probably
from
“include fastcgi.conf”), add the slash there and it all should Just
Work.

That is:

you probably have

fastcgi_param DOCUMENT_ROOT $document_root;

Change it to be

fastcgi_param DOCUMENT_ROOT $document_root/;

and on nginx -s reload, you should see the difference.

f

Francis D. [email protected]

Thanks that did the trick! Still have a lot to learn… :wink:

Posted at Nginx Forum: