# Root and alias with php5-fpm

**URL:** <https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278>\
**Category:** NGINX\
**Created:** [September 7, 2015, 6:24pm UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278 "2015-09-07T18:24:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [September 7, 2015, 6:24pm UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/1 "2015-09-07T18:24:43Z")

</div>

I am having a difficult time finding a solution for this.

I have PHP applications that may be referenced to from several websites,  
either from root locations or sub locations.

For example:

server {  
server\_name [domain.com](http://domain.com);  
location / {  
… unrelated stuff …  
}  
location /grant {  
root /apps/grant/;  
index index.php index.html;  
try\_files $uri $uri/ /grant/index.php?$args;

location ~ .php$ {  
include fastcgi\_params;  
fastcgi\_pass unix:/local/sockets/grant.sock;  
}  
}  
}

and then it may be somewhere else like this:

server {  
server\_name [another-domain.com](http://another-domain.com);  
location / {  
… unrelated stuff …  
}  
location /employ {  
root /apps/grant/;  
index index.php index.html;  
try\_files $uri $uri/ /employ/index.php?$args;

location ~ .php$ {  
include fastcgi\_params;  
fastcgi\_pass unix:/local/sockets/grant.sock;  
}  
}  
}

So the only two things different are the “location” and “try\_files”.  
However, I know that “root” will append the URI to the path, so it will  
try /apps/grant/grant/ and /apps/grant/employ/ when looking for files.  
When I use “alias”, it seems that try\_files tries looking for index.php  
in the context of “location /” on each.

The only way I can seem to resolve this is by creating a symbolic link  
at /apps/grant/grant/ and /apps/grant/employ/ pointing back to  
/apps/rant/, which I do not want. I just want each location in each  
server to see /apps/grant/ as the root, and for try\_files to process the  
index.php file in the base of that location last.

I have Lua compiled in, so I’m not sure if there are any tricks I can do  
with that to get this to work. I’m not sure what I’m missing.

Can someone provide some guidance?

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 7, 2015, 6:49pm UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/2 "2015-09-07T18:49:51Z")

</div>

Is this the issue I might be experiencing when I try to use the alias  
directive?

[https://trac.nginx.org/nginx/ticket/97](https://trac.nginx.org/nginx/ticket/97)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 7, 2015, 11:25pm UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/3 "2015-09-07T23:25:00Z")

</div>

On Mon, Sep 07, 2015 at 11:23:32AM -0500, Kristofer Pettijohn wrote:

Hi there,

> I just want each location in each  
> server to see /apps/grant/ as the root, and for try\_files to process  
> the index.php file in the base of that location last.

If I’ve understood you correctly, what you describe is not what your  
current configuration does.

Assume that files called “yes” do exist on your filesystem, and files  
called “no” do not exist.

What response do you want for requests for each of:

/grant/one/yes.txt  
/grant/one/no.txt  
/grant/one/yes.php  
/grant/one/no.php

And do you get that response in each case? If not, does the difference  
matter?

> Can someone provide some guidance?

I suggest using a named location for handling the “not there” fallback  
– either as the final argument to try\_files, or perhaps as “error\_page  
404 = @fallback”.

Then

location @fallback {  
fastcgi\_param SCRIPT\_FILENAME /apps/grant/index.php;  
include fastcgi\_params;  
fastcgi\_pass unix:/local/sockets/grant.sock;  
}

Test what order of the first two directives work in your fasctcgi  
server.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 8, 2015, 1:49am UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/4 "2015-09-08T01:49:54Z")

</div>

Thanks for the response.

> }

That is what I am attempting to do with my try\_files directive:

try\_files $uri $uri/ /grant/index.php?$args;

If the file does not exist, I want it to try “index.php” in the root of  
the grant folder, so in the try\_files directive I have  
“/grant/index.php?$args” so that it tries using the proper location. At  
least that’s how I understand try\_files?

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 8, 2015, 9:46am UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/5 "2015-09-08T09:46:03Z")

</div>

On Mon, Sep 07, 2015 at 06:48:57PM -0500, Kristofer Pettijohn wrote:

Hi there,

> > }
> 
> That is what I am attempting to do with my try\_files directive:
> 
> try\_files $uri $uri/ /grant/index.php?$args;
> 
> If the file does not exist, I want it to try “index.php” in the root of the  
> grant folder, so in the try\_files directive I have “/grant/index.php?$args” so  
> that it tries using the proper location. At least that’s how I understand  
> try\_files?

No - the last argument to try\_files is different to the other arguments.

If this last argument is used, you get an internal redirect to the url  
/grant/index.php?$args, and the appropriate location{} to handle that  
request is chosen from scratch.

Which shows exactly what you see, which is not what you want.

Does “try\_files $uri $uri/ @fallback;” do what you want?

(Rename @fallback to @grant or @grantindex if you like.)

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 9, 2015, 9:46am UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/6 "2015-09-09T09:46:42Z")

</div>

On Tue, Sep 08, 2015 at 09:23:08AM -0500, Kristofer Pettijohn wrote:

Hi there,

> When I do that, and use “root” instead of “alias” inside of  
> “location /grant”, it works. However, inside of the path I need to  
> create a symlink “ln -s . grant/” with how the root directive looks  
> for any static files. This is the part I am trying to avoid. Which  
> tells me that I should be using “alias” instead of “root”

I confess that I have become confused about what behaviour you want,  
and what behaviour you see, and what configuration you are using when  
you see the behaviour that you see.

When you make a request for /grant/one/yes.txt, what file on your  
filesystem do you want nginx to serve?

When you make a request for /grant/one/yes.php, what file on your  
filesystem do you want nginx to tell the fastcgi server to process?

When you make a request for /grant/one/no.txt, what file on your  
filesystem do you want nginx to tell the fastcgi server to process  
(because the file does not exist)?

And which of those do not do what you want, using your current  
configuration?

> But then if I use “alias”, it breaks completely, which I do not understand.
> 
> If I use @fallback, I see the same exact behavior with root vs.  
> alias, as I see when I use “/grant/index.php?$args” in try\_files.

This confuses me too. There is no “root” or “alias” in the suggested  
@fallback location. So I suspect that I am misunderstanding something.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Pettijohn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@Kristofer\_Pettijohn](https://www.ruby-forum.com/u/Kristofer_Pettijohn)\
**Post date:** [September 8, 2015, 4:24pm UTC](https://www.ruby-forum.com/t/root-and-alias-with-php5-fpm/243278/7 "2015-09-08T16:24:27Z")

</div>

> No - the last argument to try\_files is different to the other arguments.
> 
> If this last argument is used, you get an internal redirect to the url  
> /grant/index.php?$args, and the appropriate location{} to handle that  
> request is chosen from scratch.
> 
> Which shows exactly what you see, which is not what you want.

When I do that, and use “root” instead of “alias” inside of “location  
/grant”, it works. However, inside of the path I need to create a  
symlink “ln -s . grant/” with how the root directive looks for any  
static files. This is the part I am trying to avoid. Which tells me  
that I should be using “alias” instead of “root”

But then if I use “alias”, it breaks completely, which I do not  
understand.

If I use @fallback, I see the same exact behavior with root vs. alias,  
as I see when I use “/grant/index.php?$args” in try\_files.
