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

**URL:** <https://www.ruby-forum.com/t/cant-run-php-in-aliased-directory-outside-the-root-directory/217202>\
**Category:** NGINX\
**Created:** [February 23, 2012, 10:13pm UTC](https://www.ruby-forum.com/t/cant-run-php-in-aliased-directory-outside-the-root-directory/217202 "2012-02-23T22:13:13Z")\
**Posts on this page:** 2\
**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:** [February 23, 2012, 10:13pm UTC](https://www.ruby-forum.com/t/cant-run-php-in-aliased-directory-outside-the-root-directory/217202/1 "2012-02-23T22:13:13Z")

</div>

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:

> **[can't run php in aliased directory outside the root directory](https://forum.nginx.org/read.php?2,222911,222911#msg-222911)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![caleboconnell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@caleboconnell](https://www.ruby-forum.com/u/caleboconnell)\
**Post date:** [February 23, 2012, 11:38pm UTC](https://www.ruby-forum.com/t/cant-run-php-in-aliased-directory-outside-the-root-directory/217202/2 "2012-02-23T23:38:03Z")

</div>

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.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html)**

> ```
> 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.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

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