Forum: NGINX remove some parameters from $args

2974d09ac2541e892966b762aad84943?d=identicon&s=25 lahiru (Guest)
on 2013-10-06 13:00
(Received via mailing list)
Hello,
           I'm using memcached + srcache. I need to modify the $args and
take it as the cache key.

For example;

RT=62&SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK

I need to convert above $args string to below by removing parameters SID
and
UID.

RT=62&L=EN&M=1&H=1&UNC=0&SRC=LK

How can I do this ?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,243462,243462#msg-243462
37f3ea777f96500b332a1a89d6027897?d=identicon&s=25 Yichun Zhang (agentzh) (Guest)
on 2013-10-06 19:42
(Received via mailing list)
Hello!

On Sun, Oct 6, 2013 at 3:59 AM, lahiru wrote:
> Hello,
>            I'm using memcached + srcache. I need to modify the $args and
> take it as the cache key.
>

Easy if you're using the ngx_lua module at the same time. Below is a
minimal example that demonstrates this:

    location = /t {
        rewrite_by_lua '
            local args = ngx.req.get_uri_args()
            args.SID = nil
            args.UID = nil
            ngx.req.set_uri_args(args)
        ';

        echo $args;
    }

Here we use the "echo" directive from the ngx_echo module to dump out
the final value of $args in the end. You can replace it with your
ngx_srcache configurations and upstream configurations instead for
your case. Let's test this /t interface with curl:

    $ curl
'localhost:8081/t?RT=62&SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK'
    M=1&UNC=0&RT=62&H=1&L=EN&SRC=LK

Please see the related parts of the ngx_lua documentation for more
details:

    http://wiki.nginx.org/HttpLuaModule#ngx.req.get_uri_args
    http://wiki.nginx.org/HttpLuaModule#ngx.req.set_uri_args

It's worth mentioning that, if you want to retain the order of the URI
arguments, then you can do string substitutions on the value of $args
directly, for example,

    location = /t {
        rewrite_by_lua '
            local args = ngx.var.args
            newargs, n, err = ngx.re.gsub(args, [[\b[SU]ID=[^&]*&?]],
"", "jo")
            if n and n > 0 then
                ngx.var.args = newargs
            end
        ';

        echo $args;
    }

Now test it with the original curl command again, we get exactly what
you expected:

    RT=62&L=EN&M=1&H=1&UNC=0&SRC=LK

But for caching purposes, it's good to normalize the URI argument
order so that you can increase the cache hit rate. And the hash table
entry order used by LuaJIT or Lua can be used to normalize the order
as a nice side effect :)

Best regards,
-agentzh
2974d09ac2541e892966b762aad84943?d=identicon&s=25 lahiru (Guest)
on 2013-10-07 08:39
(Received via mailing list)
Works like a charm. Thank you very much Yichun.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,243462,243472#msg-243472
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.