map module regular expression help

Igor Sysoev igor at sysoev.ru
Fri Aug 26 12:02:03 UTC 2011


On Fri, Aug 26, 2011 at 12:47:55PM +0100, Dick Middleton wrote:
> Hi,
> 
> 	In the wiki for the map module (http://wiki.nginx.org/HttpMapModule) the
> following is given as an example:
> 
> map $uri $myvalue {
>     /aa                   /mapped_aa;
>     ~^/aa/(?<suffix>.*)$  /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(?<suffix>.*)$       /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.

Wiki is wrong here. Currently nginx does not support expression
in value part, you may use only a single variable, i.e.:
     ~^/pdns(?<suffix>.*)$       $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 Sysoev



More information about the nginx mailing list