Rewriting query string

classic Classic list List threaded Threaded
3 messages Options
xrd
Reply | Threaded
Open this post in threaded view
|

Rewriting query string

xrd
> rewrite ^(.*)$ $1?ra=$remote_addr break;

The ra argument is inserted before the original query string. Is it possible
to append it to the original query string?
Is this behavior documented somewhere? Couldn't find it.
Can one also remove certain arguments from the query string this way?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286625,286625#msg-286625

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Rewriting query string

Sergey A. Osokin-2
Hi Olaf,

hope you're doing well.

On Sun, Dec 29, 2019 at 04:41:58AM -0500, Olaf van der Spek wrote:
> > rewrite ^(.*)$ $1?ra=$remote_addr break;
>
> The ra argument is inserted before the original query string. Is it possible
> to append it to the original query string?
> Is this behavior documented somewhere? Couldn't find it.
> Can one also remove certain arguments from the query string this way?

The following code snippet shows another arguments has been added after
the original query:

    server {
        listen       8080;
        location / { return 200 "OK:$request_uri\n"; }
    }

    server {
        listen       80;
        location / {
                rewrite ^(.*)$ $1?ra=$remote_addr break;
                proxy_pass <a href="http://127.0.0.1:8080;">http://127.0.0.1:8080;
        }
    }
--------
% curl -i http://127.0.0.1/foo
HTTP/1.1 200 OK
Server: nginx/1.17.7
Date: Sun, 29 Dec 2019 11:51:32 GMT
Content-Type: application/octet-stream
Content-Length: 21
Connection: keep-alive

OK:/foo?ra=127.0.0.1
--------

There are several ways to manage a list of arguments of a query
string and the embedded perl module definitely can help in that
case.

Another approach is to use the following third-party module,
https://github.com/wandenberg/nginx-sorted-querystring-module

--
Sergey Osokin
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Rewriting query string

Francis Daly
In reply to this post by xrd
On Sun, Dec 29, 2019 at 04:41:58AM -0500, Olaf van der Spek wrote:

Hi there,

> > rewrite ^(.*)$ $1?ra=$remote_addr break;
>
> The ra argument is inserted before the original query string. Is it possible
> to append it to the original query string?

Something like

  rewrite ^(.*)$ $1?$args&ra=$remote_addr? break;

should work. Note that it might look odd if there was no original
query string.

Also note - common practice on the web seems to be that the order of
key=value pairs in the query string is unpredictable; you may be happier
if you change your back-end to not care about the order (if you want
random web browsers to work with your application).

> Is this behavior documented somewhere? Couldn't find it.

http://nginx.org/r/rewrite

"""
If a replacement string includes the new request arguments, the previous
request arguments are appended after them.
"""

> Can one also remove certain arguments from the query string this way?

Not easily, this way.

For that, you probably want either to use one of the embedded languages
to manipulate things; or to use the module referenced in the other reply.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx