Why does $args or $query_string not include the query parameters after &?

Viewed 33769

I am trying to set up an nginx rewrite rule to modify the URL with query request.

E.g.:

www.abc.com/pathA/pathB/script.pl?paraA=10&paraB=20

should be rewritten to:

www.abc.com/pathA/pathB/script.pl.1020

I try to use variable $args or $query_string (based on the nginx documentation they are the same) to check whether the URL has a query request. But I found $args (or $query_string) only has the contents before &.

In my example, it means $args = paraA=10. I can't get the parameter contents after &. I determine this through the nginx error log with the level set to debug.

Any idea why I can't get the query parameters after & in variable $args or $query_string?

2 Answers

The problem is not caused by nginx configuration file. It is caused by I forget double quote when I sent request using curl. E.g.: curl "www.abc.com/pathA/pathB/script.pl?paraA=10&paraB=20" Without double quote, "&paraB=20" never be sent actually.

You need to use $arg_<argname>

www.abc.com/pathA/pathB/script.pl?paraA=10&paraB=20 will be rewritten to : www.abc.com/pathA/pathB/script.pl.1020

Below is a sample config to do such a thing

location /pathA/pathB/script.pl {
    set $need_redirect "";
    if ($arg_paraA) {
       set $need_redirect "A";
    }

    if ($arg_paraB) {
       set $need_redirect "B$need_redirect";
    }

    if ($need_redirect = "BA") {
       rewrite ^(.*)$ $1.$arg_paraA$arg_paraB;
    }
}
Related