# Map module regular expression help

**URL:** <https://www.ruby-forum.com/t/map-module-regular-expression-help/211132>\
**Category:** NGINX\
**Created:** [August 26, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/map-module-regular-expression-help/211132 "2011-08-26T13:48:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 26, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/map-module-regular-expression-help/211132/1 "2011-08-26T13:48:36Z")

</div>

Hi,

In the wiki for the map module ([Module ngx\_http\_map\_module](http://wiki.nginx.org/HttpMapModule))  
the  
following is given as an example:

map $uri $myvalue {  
/aa /mapped\_aa;  
~^/aa/(?.\*)$ /mapped\_bb/$suffix;  
}

The second form, the regex, I can’t get to work - it returns the literal  
text  
rather than substituting for $suffix.

from my map block:

map $uri $locn {  
~^/pdns(?.\*)$ /var/www/pdns$suffix;  
}

the I rewrite:

rewrite ^ $locn;

from the log:

open() “/var/www/pdns$suffix” failed

How do I get this to substitute?

This is using nginx/1.1.0 on debian squeeze.

Dick

---

<div class="post-metadata">

**Author:** ![dmonahan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dmonahan](https://www.ruby-forum.com/u/dmonahan)\
**Post date:** [August 26, 2011, 2:02pm UTC](https://www.ruby-forum.com/t/map-module-regular-expression-help/211132/2 "2011-08-26T14:02:48Z")

</div>

On Fri, Aug 26, 2011 at 12:47:55PM +0100, Dick M. wrote:

> rewrite ^ $locn;
> 
> from the log:
> 
> open() “/var/www/pdns$suffix” failed
> 
> How do I get this to substitute?
> 
> This is using nginx/1.1.0 on debian squeeze.

Wiki is wrong here. Currently nginx does not support expression  
in value part, you may use only a single variable, i.e.:  
~^/pdns(?.\*)$ $suffix;

However, this wiki example is very ineffectual, because it maps $uri to  
path  
forcing to use “rewrite”. There are other simple ways to this, for  
example:

```
location /pdns {
    root /var/www;
}

```

or (wiki expample):

```
location = /aa {
    alias /mapped_aa;
}

location /aa/ {
    alias /mapped_bb/;
}

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![dmonahan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dmonahan](https://www.ruby-forum.com/u/dmonahan)\
**Post date:** [August 26, 2011, 6:05pm UTC](https://www.ruby-forum.com/t/map-module-regular-expression-help/211132/3 "2011-08-26T18:05:48Z")

</div>

On 08/26/11 13:02, Igor S. wrote:

> On Fri, Aug 26, 2011 at 12:47:55PM +0100, Dick M. wrote:
> 
> > Hi,
> > 
> > In the wiki for the map module ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpMapModule)) the  
> > following is given as an example:
> > 
> > map $uri $myvalue {  
> > /aa /mapped\_aa;  
> > ~^/aa/(?.\*)$ /mapped\_bb/$suffix;  
> > }

> Wiki is wrong here. Currently nginx does not support expression  
> in value part, you may use only a single variable, i.e.:

I thought that might be it 😉

What I’m trying to do is make my config file more elegant. I have quite  
a  
number of subdirectories which all have their own document root. So  
there’s a  
location for each and each has to have an alias and a php proxy. I was  
trying  
to coalesce them.

So I tried:

map $uri $locn {  
/sub1 /locn1;  
/sub2 /locn2;

```
 ...

```

}

index index.php;  
location ~ ^/(sub1|sub2 etc ) {  
alias $locn;

location ~ .php$ {

}  
}

but it doesn’t work. fastcgi can’t find the file. One symptom is that  
$request\_filename is not set to the full path; i.e the index.php part is  
missing.

It works OK if I put a literal in the alias rather than the same value  
from a  
mapped variable.

Is this construct supported?

There’s another problem I don’t understand

If I have

index index.php;

location /sub1 {  
alias /path1;  
…  
}

It looks for /path1/index.php as expected but if I change location to  
regex

location ~ ^/sub1

then it looks for /path1 with no index.

Dick
