Nginx Map Multiple Parameter Input

I have a case where I need to use Nginx map in slightly different way.

I use following to create a variable $blogpath

map $uri $blogname{ ~^(?P<blogpath>/[_0-9a-zA-Z-]+/)files/(.*) $blogpath ; }

Next, I want to run another map using:

map $http_host$blogpath $blogid{ #map lines }

Problem is - map doesn’t support 2 input parameters. It throws error:
“nginx: [emerg] invalid number of the map parameters …”

As map is outside server{] block, I cannot use set to create a temporary
variable to combine value of “$http_host$blogpath”

Goal is to get “domain-name.com/first-dir” from URL. If any other nginx
variable can give me entire URL as seen in browser, it will also work.

Please suggest a workaround!

Posted at Nginx Forum:

On Oct 12, 2012, at 16:26 , rahul286 wrote:

variable to combine value of “$http_host$blogpath”

Goal is to get “domain-name.com/first-dir” from URL. If any other nginx
variable can give me entire URL as seen in browser, it will also work.

Please suggest a workaround!

What version do you use ?

Changes with nginx 0.9.0 29 Nov
2010

   ...

*) Feature: the "map" directive supports expressions as the first
   parameter.


Igor S.

I am using Nginx 1.2.4

So a line like “map $http_host$uri $blogid” is valid one?

Posted at Nginx Forum:

On Oct 12, 2012, at 17:14 , rahul286 wrote:

I am using Nginx 1.2.4

So a line like “map $http_host$uri $blogid” is valid one?

Yes.

This message is issued for internal map parameters.
Probably you have space in some first or second paramter.


Igor S.

I will debug my config again. Thanks.

By the way, does map support regex in input parameter? Now or in future?

Posted at Nginx Forum:

On Oct 12, 2012, at 18:21 , rahul286 wrote:

I will debug my config again. Thanks.

By the way, does map support regex in input parameter? Now or in future?

Changes with nginx 0.9.6 21 Mar
2011

*) Feature: the "map" directive supports regular expressions as 

value of
the first parameter.

http://nginx.org/en/docs/http/ngx_http_map_module.html

map $http_user_agent $mobile {
default 0;
“~Opera Mini” 1;
}


Igor S.

On Oct 12, 2012, at 18:37 , rahul286 wrote:

Sorry for wrong question. I wanted to ask…

map “~ ^/~([^/])/.$)” $userhome{
}

style regex. Where map input string will be $1.

No. “map” is not “location”.

I get error: “nginx: [emerg] unknown “1” variable” when I try:

map $uri $blogname{
~^(/[^/]+/)files/(.*)$ $1 ;
}

Digit captures are not supported in map. You have to use named capture.


Igor S.

Sorry for wrong question. I wanted to ask…

map “~ ^/~([^/])/.$)” $userhome{
}

style regex. Where map input string will be $1.

===

Apart from that, inside map:

map $uri $blogname{
~^(?P/[^/]+/)files/(.*)$ $blogpath ;
}

I am not able to use $1. I always have to use a variable like $blogpath.
Is
it by design or a mistake on my end?

I get error: “nginx: [emerg] unknown “1” variable” when I try:

map $uri $blogname{
~^(/[^/]+/)files/(.*)$ $1 ;
}

Posted at Nginx Forum:

Igor S. Wrote:

Digit captures are not supported in map. You have to use named capture.

Thanks for clarification :slight_smile:

Is map exception or there are other directives also which do not support
Digit captures?

I ran into issues in if-location block (mostly an If-evil case). I
switched
to named capture and it worked nice.

Thanks again.

Posted at Nginx Forum:

On Oct 12, 2012, at 18:51 , rahul286 wrote:

to named capture and it worked nice.
Some directives support, but digital captures can be implicitly
overwritten.
With named captures you can control explicitly.


Igor S.

On Oct 12, 2012, at 19:14 , rahul286 wrote:

Yep. When using values across lines, I noticed overwriting.

One last question:

I think using “P” in named capture e.g. “?P” is old style. But
even on nginx 1.2 also some people get error: “pcre_compile() failed:
unrecognized character after”. It goes away when they update PCRE lib.

For better compatibility, is it good idea to use “P” or is there any
advantage without “P”?

No advantage, just readability.

? Perl 5.10 compatible syntax, supported since PCRE-7.0
?‘name’ Perl 5.10 compatible syntax, supported since PCRE-7.0
?P Python compatible syntax, supported since PCRE-4.0

BTW PCRE-7.0 has been released on 19 December 2006, almost 6 years ago.


Igor S.

Thanks again for more details.

For better compatibility, I will use “P” everywhere. :slight_smile:

Posted at Nginx Forum:

Yep. When using values across lines, I noticed overwriting.

One last question:

I think using “P” in named capture e.g. “?P” is old style. But
even on nginx 1.2 also some people get error: “pcre_compile() failed:
unrecognized character after”. It goes away when they update PCRE lib.

For better compatibility, is it good idea to use “P” or is there any
advantage without “P”?

Posted at Nginx Forum: