# What's the simplest way to serve php files through an alias?

**URL:** <https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927>\
**Category:** NGINX\
**Created:** [October 3, 2010, 1:07am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927 "2010-10-03T01:07:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 1:07am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/1 "2010-10-03T01:07:07Z")

</div>

we have a very simple configuration.

server block with location block

php is served through fastcgi and works fine

would like to add something like this:

location /nameofalias/  
{  
alias /usr/local/nameofalias;  
}

but it does not seem to work, static files load fine.

the php location block is like this and works fine for the main location  
block of “location /”

```
    location ~ .php$
    {
            include fcgi;
            fastcgi_pass 127.0.0.1:9000;
    }

```

what else is needed?

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 1:34am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/2 "2010-10-03T01:34:37Z")

</div>

On 3 Out 2010 00h05 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> alias /usr/local/nameofalias;  
> }

This is now how alias is supposed to be used. In fact you’re using  
alias like a root directive. Using alias is for when you want to use a  
certain base directory and don’t want the URI to reflect that.

location /foo {  
alias /var/www/nginx-default/barz;  
}

Request for /foo/xpto.png is translated to

/var/www/nginx-default/barz/xpto.png

> but it does not seem to work, static files load fine.

> the php location block is like this and works fine for the main  
> location block of “location /”
> 
> location ~ .php$  
> {  
> include fcgi;  
> fastcgi\_pass 127.0.0.1:9000;  
> }

You should have a fastcgi\_pass directive where you want the PHP files  
to be handled.

Also you need to escape the “.” as in

location ~ .php$ {  
(…)  
}

— appa

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 2:24am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/3 "2010-10-03T02:24:23Z")

</div>

Thanks,

I tried it using “root” in the location, still no luck.

Here’s my config (the parts that matter), the location /nagios/ is  
what’s  
not working. What am I doing wrong? Thanks

http  
{  
root /var/www/html;  
server  
{  
listen 10.0.1.163;  
server\_name [dev.testsite.com](http://dev.testsite.com);  
location /nagios/  
{  
root /usr/local/nagios/share;  
index index.php;  
}  
location /  
{  
index index.php;  
error\_page 404 = @joomla;  
log\_not\_found off;  
}  
location @joomla  
{  
rewrite ^(.\*)$ /index.php?q=$1 last;  
}

```
    location ~ \.php$
    {
            include fcgi;
            fastcgi_pass 127.0.0.1:9000;
    }

}

```

}

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 3:04am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/4 "2010-10-03T03:04:45Z")

</div>

On Sun, Oct 3, 2010 at 7:23 AM, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> I tried it using “root” in the location, still no luck.  
> Here’s my config (the parts that matter), the location /nagios/ is what’s  
> not working. Â What am I doing wrong? Thanks

“not working”?

- What are you expecting?
- What are you getting?

> Â Â Â Â Â Â Â Â Â index index.php;  
> Â Â Â Â Â }

as this one is located in different root path, you need additional php  
block

location ~ ^/nagios/.\*.php$ {  
…  
}

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:32am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/5 "2010-10-03T06:32:26Z")

</div>

Thanks for the advice. When I do it this way, I get an error (nginx  
error  
log):

2010/10/03 00:27:33 [error] 20239#0: \*6447  
“/usr/local/nagios/share/nagios/index.php” is not found (2: No such file  
or  
directory)

so you can see that nginx is adding /nagios/ to the end of the root  
directory and so the file is not found (generating a 404 on the  
browser).

What would be the correct way to address this issue?

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:34am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/6 "2010-10-03T06:34:12Z")

</div>

Thank you for your suggestion, I remember the vigorous discussion that  
took  
place regarding this issue, in this particular case, this is a test /  
development system so I’m not as concerned as I normally would be.

In our production environment, things are a bit more rigid.

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:40am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/7 "2010-10-03T06:40:03Z")

</div>

btw, when I change the same configuration to alias and try to access  
/nagios/, I get this error:

2010/10/03 00:36:06 [error] 7399#0: \*6485 directory index of  
“/usr/local/nagios/share” is forbidden

when I go to /nagios/index.php, I don’t get an error in the log, but an  
error in the browser, that says:  
Oops! This link appears to be broken.

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 3:34am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/8 "2010-10-03T03:34:00Z")

</div>

On 3 Out 2010 01h23 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> {  
> location /  
> location ~ .php$  
> {  
> include fcgi;  
> fastcgi\_pass 127.0.0.1:9000;  
> }
> 
> }  
> }

For security reasons I suggest you constrain which exact locations can  
be used for FastCGI. Using a generic regex for any file with php  
extension opens a big security hole. This was discussed not long ago  
on the list.

Instead you should enumerate which files are to be handled by FastCGI  
and return a 404 for every other file that is not enumerated. E.g.,

location ~\* ^/index.php$ {  
include fcgi;  
fastcgi\_pass 127.0.0.1:9000;  
}

And put at the end of the config file:

# Any other attempt to access PHP files returns a 404.

location ~\* ^.+.php$ {  
return 404;  
}

— appa

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:41am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/9 "2010-10-03T06:41:13Z")

</div>

On 3 Out 2010 05h31 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> browser).
> 
> What would be the correct way to address this issue?

Using alias:

```
    location /nagios {
            alias /usr/local/nagios/share;
            index index.php;
    }

```

Request for nagios gets translated in /usr/local/nagios/share/index.php;

HTH,  
— appa

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:41am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/10 "2010-10-03T06:41:48Z")

</div>

On Sun, Oct 3, 2010 at 11:31 AM, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks for the advice. Â When I do it this way, I get an error (nginx error  
> log):  
> 2010/10/03 00:27:33 [error] 20239#0: \*6447  
> “/usr/local/nagios/share/nagios/index.php” is not found (2: No such file or  
> directory)  
> so you can see that nginx is adding /nagios/ to the end of the root  
> directory and so the file is not found (generating a 404 on the browser).  
> What would be the correct way to address this issue?

> ```
> location /nagios/
> {
> root /usr/local/nagios/share;
> index index.php;
> }
> 
> ```

use alias.

location ~ ^/nagios/(.\*.php)$ {  
alias /usr/local/nagios/share/$1;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include fastcgi\_params;  
}  
location = /nagios {  
rewrite ^ /nagios/ permanent;  
}  
location /nagios/ {  
index index.php;  
alias /usr/local/nagios/share/;  
}

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![iberci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a3d4f5/32.png) [@iberci](https://www.ruby-forum.com/u/iberci)\
**Post date:** [October 3, 2010, 6:51am UTC](https://www.ruby-forum.com/t/whats-the-simplest-way-to-serve-php-files-through-an-alias/195927/11 "2010-10-03T06:51:10Z")

</div>

Thank you all for your responses.

I believe that the missing element in my case was:

alias /usr/local/nagios/share/$1;

in the nagios php processing location, I wasn’t aware that I would have  
to  
specify it there as well, but it makes sense now.

Thanks!
