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

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?

On 3 Out 2010 00h05 WEST, [email protected] 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

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

}

}

On Sun, Oct 3, 2010 at 7:23 AM, Ilan B. [email protected] 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

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?

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.

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.

On 3 Out 2010 01h23 WEST, [email protected] 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

On 3 Out 2010 05h31 WEST, [email protected] 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

On Sun, Oct 3, 2010 at 11:31 AM, Ilan B. [email protected]
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

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs