Create custom variable based on another one

pixeye nginx-forum at
Thu Aug 4 12:27:53 UTC 2016

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 :

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

Here is some of my attemps (please don't laugh), not working : 
~/^\/(?P<pathname>[^\/]+)/? $pathname;
~/^\/(?P<pathname>)[^\/]+/? $pathname;
~/^[\/+](?P<pathname>[^\/]+)/? $pathname;
~/^[\/+](?P<pathname>)[^\/]+/? $pathname;

To be clear on what i expect :
/a for /a/b/c.png
/a for /a/c.png
/ for /c.php

Thanks in advance !

Posted at Nginx Forum:,268761,268761#msg-268761

More information about the nginx mailing list