# Trailing slash being removed from document root

**URL:** <https://www.ruby-forum.com/t/trailing-slash-being-removed-from-document-root/215454>\
**Category:** NGINX\
**Created:** [January 4, 2012, 11:31pm UTC](https://www.ruby-forum.com/t/trailing-slash-being-removed-from-document-root/215454 "2012-01-04T23:31:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 4, 2012, 11:31pm UTC](https://www.ruby-forum.com/t/trailing-slash-being-removed-from-document-root/215454/1 "2012-01-04T23:31:26Z")

</div>

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:

> **[Trailing slash being removed from document root](https://forum.nginx.org/read.php?2,220811,220811#msg-220811)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![freejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@freejack](https://www.ruby-forum.com/u/freejack)\
**Post date:** [January 4, 2012, 11:57pm UTC](https://www.ruby-forum.com/t/trailing-slash-being-removed-from-document-root/215454/2 "2012-01-04T23:57:21Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![freejack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@freejack](https://www.ruby-forum.com/u/freejack)\
**Post date:** [January 5, 2012, 12:17am UTC](https://www.ruby-forum.com/t/trailing-slash-being-removed-from-document-root/215454/3 "2012-01-05T00:17:08Z")

</div>

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

Posted at Nginx Forum:

> **[Re: Trailing slash being removed from document root](https://forum.nginx.org/read.php?2,220811,220814#msg-220814)**
>
> 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:...
