map module regular expression help

Dick Middleton dick at fouter.net
Fri Aug 26 16:05:01 UTC 2011


On 08/26/11 13:02, Igor Sysoev wrote:
> 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;
>> }

> 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





More information about the nginx mailing list