Create custom variable based on another one

Edho Arief me at myconan.net
Thu Aug 4 12:58:04 UTC 2016


Hi,

On Thu, Aug 4, 2016, at 21:27, pixeye wrote:
> Hello there,
> 
> I'm trying to set a new variable based on another one.
> 
> I've found this response which is kinda close to what i want to do :
> https://www.ruby-forum.com/topic/6876231#1176774
> 
> map $request_uri $last_path {
>   ~/(?<pathname>[^/]+)/?$ $pathname;
> }
> 
> Except, i want to get the first path level only (and not the file name) !
> 
> It seems to work fine even if the syntax seems not really correct, here
> is
> what i came up with (and it's working) : 
> 
> map $request_uri $last_path {
>   ~/(?P<pathname>[^\/]+)/?$ $pathname;
> }
> 
> But, as soon as i try to make changes in order to get the first path, my
> $last_path value is empty...
> I guess i didn't succed in making all the changes required for it to
> work...
> 
> Here is some of my attemps (please don't laugh), not working : 
> ~/^\/(?P<pathname>[^\/]+)/? $pathname;
> ~/^\/(?P<pathname>)[^\/]+/? $pathname;
> ~/^[\/+](?P<pathname>[^\/]+)/? $pathname;
> ~/^[\/+](?P<pathname>)[^\/]+/? $pathname;
> 

`~` already starts the regexp and forward slash doesn't need to be
escaped.

Also, depends on your definition of "file name":

map ... {
  ~^(/[^/]+)/ $1;
  default /;
}

map ... {
  ~^(/[^/.]+)(/|$) $1;
  default /;
}



More information about the nginx mailing list