Remove some parameters from $args

I’m using memcached + srcache. I need to modify the $args and
take it as the cache key.

For example;


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


How can I do this ?

Posted at Nginx Forum:,243462,243462#msg-243462

Works like a charm. Thank you very much Yichun.

Posted at Nginx Forum:,243462,243472#msg-243472


On Sun, Oct 6, 2013 at 3:59 AM, lahiru wrote:

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

    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 


Please see the related parts of the ngx_lua documentation for more

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 =, [[\b[SU]ID=[^&]*&?]], 

“”, “jo”)
if n and n > 0 then
ngx.var.args = newargs

    echo $args;

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


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 :slight_smile:

Best regards,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs