Forum: NGINX nginx cookie - how to inject variable into cookie

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Payam C. (Guest)
on 2009-05-31 13:22
(Received via mailing list)
Hey,

im trying to compare the cookie with  the remote_addr via nginx but
nginx sees $remote_addr as the text and not the a variable ... so it
wont match properly
I do have two cookies... the one I need to match for is the 2nd cookie
"YPF8827340282Jdskjhfiw_928937459182JAX666=96.48.77.222"

                if ( $http_cookie !~
YPF8827340282Jdskjhfiw_928937459182JAX666=$remote_addr ) {
                        rewrite  ^(.*)$  /check/$host.shtml  last;
                        break;
                }

2009/05/31 02:01:29 [debug] 1775#0: *6 http script regex:
"YPF8827340282Jdskjhfiw_928937459182JAX666=$remote_addr"
2009/05/31 02:01:29 [notice] 1775#0: *6
"YPF8827340282Jdskjhfiw_928937459182JAX666=$remote_addr" does not
match "YPF8827340282Jdskjhfiw_9289374591823=2;
YPF8827340282Jdskjhfiw_928937459182JAX666=96.48.77.222", client:
96.48.77.222, server: www.whatwentwrong.org, request: "GET /index.html
HTTP/1.1", host: "www.whatwentwrong.org", referrer:
"http://www.whatwentwrong.org/index.html"

any ideas?

Thanks in advance
Maxim D. (Guest)
on 2009-06-01 14:58
(Received via mailing list)
Hello!

On Sun, May 31, 2009 at 02:05:12AM -0700, Payam C. wrote:

>                         rewrite  ^(.*)$  /check/$host.shtml  last;
>                         break;
>                 }

This wont work since nginx doesn't allow variables in a right-hand side
of the expression.  Workaround is to use something like this:

    set $blah "$remote_addr:$http_cookie";
    if ($blah !~
"^([0-9.]+):.*YPF8827340282Jdskjhfiw_928937459182JAX666=\1") {
        ...
    }

This uses static pattern with pcre builtin backreference and will
work as expected.

Maxim D.
Payam C. (Guest)
on 2009-06-02 07:11
(Received via mailing list)
On Mon, Jun 1, 2009 at 3:45 AM, Maxim D. <removed_email_address@domain.invalid> 
wrote:
>> "YPF8827340282Jdskjhfiw_928937459182JAX666=96.48.77.222"
>    set $blah "$remote_addr:$http_cookie";
>    if ($blah !~ "^([0-9.]+):.*YPF8827340282Jdskjhfiw_928937459182JAX666=\1") {
>        ...
>    }
>
> This uses static pattern with pcre builtin backreference and will
> work as expected.
>
> Maxim D.
>
>

Maxim,

Thanks much ... worked out great =)
This topic is locked and can not be replied to.