1

How do I use nginx to transform incoming query string parameters according to some rule? In particular, I want to prepend a prefix onto each and every query string parameter value. In particular, I would like to do the following.

Given this URI:

http://localhost:3000/account?id=12345&name=bill

I would like it to be such that this is transformed into

http://localhost:3000/account?id=eq.12345&name=eq.bill

Where the prefix in each item is eq. The reason is that I'm using nginx as a reverse proxy for PostgREST, and PostgREST has a rich URL syntax built around operators like eq., but I need to use it with clients who don't know that syntax.

I've looked into the nginx rewrite and the nginx map module. The latter looks promising but I haven't yet figured out out to use it.

I did also try some lua scripting, to try to loop over npx.req.get_uri_args but at present I'm debugging the Lua code.

events {
}

http {
    resolver 127.0.0.11 ipv6=off;
    server {
        listen 80;
        listen [::]:80;
        location / {
            default_type text/plain;
            access_by_lua_block {
               local qs = {}
               for key, val in pairs(ngx.req.get_uri_args()) do
                  table.insert(qs, key, "eq." .. val)
               end
               ngx.req.set_uri_args(qs)
            }            
            proxy_pass http://postgrest:3000;
        }
    }
}

1 Answers1

2

I believe I answered my own question, though I welcome more input and contributions. The following Lua scripting worked in nginx.conf:

events {
}

http {
    resolver 127.0.0.11 ipv6=off;
    server {
        listen 80;
        listen [::]:80;
        location / {
            access_by_lua_block {
               local qs = {}
               for key, val in pairs(ngx.req.get_uri_args()) do
                  qs[key] = "eq." ..val
               end
               ngx.req.set_uri_args(qs)
            }            
            proxy_pass http://postgrest:3000;
        }
    }
}

I'd love to know if there are alternatives in nginx to Lua scripting, but this seemed to do the trick.