Can't run php in aliased directory outside the root directory

A quick explanation as to what I’m trying to accomplish.
We have a website where we want to keep a media folder outside the root.
This holds all of our large images and videos and mp3 files. We don’t
want to keep this in our git repo for deployment since it’s only used on
the live site. We’ve used root to change the root location of a path
name.

Recently we’ve wanted to store some php files in this media folder, in a
sub-directory to work as front ends to some large swf files we’re
hosting. I’ve added an additional location block to hopefully match the
request to this sub-directory in the media folder to host some php
files. When I go to the page, instead of processing the php, it just
downloads the file.

Below is the config section of our sites-available file that defines the
site.
The actual path to the PHP fie we want to run is:
/var/www/media/courses/OCT-CIPPE/player.php.
The path will always be /var/www/media/courses but the OCT-CIPPE will
change based on the program we want to host. The php file will always
be titled player.php.

Any help/suggestions as to how to change this so it will see the php
file as a file it passes to php5-fpm.

Web conference alias and flash video settings

location ^~ /media {
root /var/www;
}

location ~ /media/courses/..php$ {
root /var/www;
if ($fastcgi_script_name ~ /media/courses(/.
.php)$) {
set $valid_fastcgi_script_name $1;
}
fastcgi_pass unix:/tmp/phpfpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
}

Posted at Nginx Forum:

On Thu, Feb 23, 2012 at 04:12:49PM -0500, caleboconnell wrote:

Hi there,

There is an issue with using php in an aliased location; but you don’t
seem to use “alias” in your configuration at all, so that’s not the
problem here.

location ^~ /media {
The “^~” there means that, if this is the most specific prefix-match
location, the regex locations are not checked.

http://www.nginx.org/en/docs/http/ngx_http_core_module.html#location

root /var/www;

}

location ~ /media/courses/.*.php$ {

And this one is a regex location. So this is not used at all. Which is
why your php script is served as-is, without fastcgi processing.

From what you say above, this regex could be tightened to require
/player.php at the end. And maybe to require no / between courses/
and /player.php

root /var/www;
if ($fastcgi_script_name ~ /media/courses(/.*\.php)$) {
 set $valid_fastcgi_script_name $1;
}

I’m not sure what that part tries to do; it’s an “if” inside a
“location”,
so it probably does not do what you expect.

You don’t seem to use $valid_fastcgi_script_name anyway.

I suspect that if you move the “location ~” block inside the “location
^~” block, you’ll either see things Just Work, or at least see some more
useful error messages to point you at any other problems.

The “root” and “if” parts above are then probably ok to remove.

fastcgi_pass unix:/tmp/phpfpm.sock;
fastcgi_param SCRIPT_FILENAME  /var/www$fastcgi_script_name;
include fastcgi_params;

And the last two lines could become “include fastcgi.conf;”, or
alternatively $document_root could be used instead of /var/www.

Good luck with it,

f

Francis D. [email protected]