Root and alias with php5-fpm

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;
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;
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?

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

https://trac.nginx.org/nginx/ticket/97

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. [email protected]

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?

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. [email protected]

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. [email protected]

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.