Regex matching on a cookie

Hello,

I have a URI like this:

example.com/cnvpb/$IntegerA/$TermA

eg:
example.com/cnvpb/1233/Q4

For this request , I might get cookies with names like

abc_cnv1, abc_cnv2, abc_cnv3…abc_cnv5

all or some of them. The value of these cookies are of the format:

$integerB#$TermB#$integerC#$TermC

Eg:
1234#blahblah#45#Q1

Now this is the requirement:

If for any of the cookies in abc_cnv[1-5], $integerA equals $intergerB
choose an upstream based on $TermC.

I have figured out few of things:

  1. Parsing $integerA - Using a regex in the location block
  2. Choosing an upstream based on $TermC - using a map block

However, I’m stuck at reading cookies (not sure how to read only :
abc_cnv[1-5]). I understand it can be done using multiple ifs .
But I’m sure there should be a better way than that.

Any pointers?

Thanks
Harish

On Monday 03 September 2012 19:34:25 Harish S. wrote:
[…]

However, I’m stuck at reading cookies (not sure how to read only :
abc_cnv[1-5]). I understand it can be done using multiple ifs .
But I’m sure there should be a better way than that.

Any pointers?

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

$cookie_abc_cnv1, $cookie_abc_cnv2, $cookie_abc_cnv1…

wbr, Valentin V. Bartenev

Hi Valentin,

Module ngx_http_core_module

$cookie_abc_cnv1, $cookie_abc_cnv2, $cookie_abc_cnv1…

Ah. I did know how to read it individually. My mistake, should have
explained it more clearly. But testing for each of this variables
looks bad (is ther a way other than using a if clause ? )

Thanks
Harish

Hi all,

After a bit of head scratching, I came up with this:

  1. Parse $integerA from the URI
  2. Have two map blocks.
    a) one to get $TermC from the cookie
    b) one to map a backend based on the $TermC

Here is what I have got:

    map $http_cookie $termC {
            ~adq_cnv(\d+)=($integer[^;]+#(\w{2}))(?:;|$) $3;
            default "WX";
    }

   map $termC $backend {
        WX w.x.y.z;
        AB a.b.c.d;
   }

location ~ ^/cnvpb/(\d+)/ {
set $cmpid $1;
proxy_pass http://$backend;
}

But unfortunately it fails saying - “invalid number of map
parameters”. I think its something to do with variable interpolation.
So a few questions:

  1. How do I substitute a variable inside a regex ?
  2. The regex works with a if condition. I kind of extended it from
    here: Module ngx_http_rewrite_module. But there seems to
    be a “space” between “[^;]” and “+” not sure why. Is that right?
  3. Anything else that can be done better?

Thanks
Harish

Hi all,
Sorry that location block should have been:

location ~ ^/cnvpb/(\d+)/ {
set $integer $1;
proxy_pass http://$backend;
}

Thanks
Harish