Use /g (global) regex modifier in map

Maxim Dounin mdounin at mdounin.ru
Fri Aug 8 18:46:57 UTC 2014


Hello!

On Fri, Aug 08, 2014 at 08:27:54PM +0200, Gabriel Arrais wrote:

> Maxim Dounin wrote in post #1154691:
> > Hello!
> >
> > On Fri, Aug 08, 2014 at 05:06:55PM +0200, Gabriel Arrais wrote:
> >
> >> The same problem would occur using the variable $args right?
> > Sure.
> >
> >> Example:
> >>         ?fq=xxxxxx&sm=0&PageNumber=1     and
> >>         ?fq=xxxxxx&sm=0&PageNumber=1&fq=yyyyyyyyyy
> >>
> >> Are returning the same content. Is there a way to avoid this behavior?
> >
> > There is no easy one, as nginx itself doesn't know how to work
> > with multiple arguments with the same name.  You may try to build
> > a regex to extract second argument with the given name (3rd, 4th,
> > and so on) and include these into the cache key as well.
> >
> 
> I think that it would end in a complicated solution this way...
> 
> Do you think that a perl code like
> 
> perl_set  $filtered_args '
>   sub {
>     my $r = shift;
>     my $args=$r->args;
> 
>     my @parts = $args =~ /[DESIRED_REGEX]/g;
>     @parts = sort @parts;
>     $filtered_args = join("&", @parts);
>     return $filtered_args;
>   }
> ';
> proxy_cache_key  $host:$uri?$filtered_args;
> 
> Will do the job?

Doing this with embedded perl snippet will be more or less 
trivial, yes.  Note though that in case of multiple arguments with 
the same name it may be important to preserve their order.

I also suspect that split() + grep may be better/easier than a 
single regular expression to match all needed arguments.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list