Use /g (global) regex modifier in map

Maxim Dounin mdounin at
Fri Aug 8 11:09:56 UTC 2014


On Fri, Aug 08, 2014 at 08:37:39AM +0200, Gabriel Arrais wrote:

> Is it possible somehow to use the global modifier in a regex map match?
> I'm trying to use the map directive to filter the query string leaving
> my proxy_cache_key with only known parameters.
> For the first test I've tried to use a map like below to just catch all
> parameters without really filtering them without success. In this case,
> the variable $args_filtered ends empty.
>     map $args $args_filtered {
>         "~(?<list>[^=]*=[^&]+)/g"  $list;
>         default  /;
>     }
> When I try the same map, without the /g modifier at the end of the
> expression, the variable $args_filtered ends with only the first query
> string parameter in it.

No, it's not supported.  To use "/g", one have to do regexp 
matching multiple times and do something with the results of each 
match, and this isn't something nginx knows how to do.  |(In perl, 
this usually what happens automatically in substitution, "s///", 
but needs writing code when matching with "m//".)

Note well that even if you'll be able to filter arguments, there 
is an additional problem of order of the arguments.

Simpliest way to normalize arguments is to use all of them in 
proxy_cache_key, like this:

    proxy_cache_key $proxy_host$uri$is_args$arg_foo:$arg_bar;

Maxim Dounin

More information about the nginx mailing list