Equivalent to "dirname"

Hello,

I would like to check if the directory of a non existant file exist.

/mydir/poipoi/foobar doesn’t exist (easy to test)
/mydir/poipoi/ <-- I need to check that

Basically it would take a “dirname” function.

Can we do that with nginx ?
Maybe it’s over the scope of a webserver…

Thanks !

Pior B.
[email protected]

On Mon, Dec 14, 2009 at 12:09 PM, Pior B. [email protected]
wrote:

Maybe it’s over the scope of a webserver…

Hello,

The rewrite module provides a -d operator to the if directive which
will test for a directory existence; please see
Module ngx_http_rewrite_module .

Alternatively, if you are going to end up using the index or some
value inside the directory if it exists, then you should consider
using the try_files directive documented here:
Module ngx_http_core_module .

Thanks,
Merlin

On December 14, 2009 03:20:20 pm merlin corey wrote:

On Mon, Dec 14, 2009 at 12:09 PM, Pior B. [email protected] wrote:

Hello,

I would like to check if the directory of a non existant file exist.

The rewrite module provides a -d operator to the if directive which
will test for a directory existence; please see
Module ngx_http_rewrite_module .

Alternatively, if you are going to end up using the index or some
value inside the directory if it exists, then you should consider
using the try_files directive documented here:
Module ngx_http_core_module .

Thank you Merlin.

I almost know all the wiki, including -d and try_files.

Here is what I need to do :

  1. try to server /poipoi/foobar
  2. if the file doesn’t exist, check if /poipoi/ exist
  3. if this directory doesn’t exist, return a redirect, otherwise return
    404

How can I get the base directory of $request_filename ?


Pior B.
System A.
Mentel Inc
[email protected]

On Mon, Dec 14, 2009 at 12:59 PM, Pior B. [email protected] wrote:

Alternatively, if you are going to end up using the index or some

  1. try to server /poipoi/foobar

Pior,

Sorry for confusion! I don’t think you can directly do what you want
do, in that there is no specific “dirname function” (there isn’t even
a concept of a function in the nginx configuration language right
now).

That said you should be able to do what you want using regular
expression captures. Something like the following might work for your
needs, but I have not tested it and I am the best with regex:

location ~ ^(.)/(.)$ {
if (-d $1) {
rewrite ^ http://otherplace;
}
}

Thanks,
Merlin