Forum: NGINX Stripping out query parameter?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Neil Sheth (Guest)
on 2009-03-03 09:21
(Received via mailing list)
Hello,

I'm trying to rewrite some urls that are incorrect.  An example would
be:

www.mysite.com/test.php?x=123&extra=yes

I want to rewrite this to

www.mysite.com/test.php?x=123

I'm trying something like the following, without luck:

         if ($args ~* ^(.*)(&?)extra=yes$) {
             set $stripped_params = $1;
             rewrite ^(.*)$ $1$stripped_params permanent;
         }

Any thoughts?  I'm running 0.6.31.  Thanks!
Igor Sysoev (Guest)
on 2009-03-03 10:26
(Received via mailing list)
On Tue, Mar 03, 2009 at 12:08:11AM -0800, Neil Sheth wrote:

> I'm trying something like the following, without luck:
>
>          if ($args ~* ^(.*)(&?)extra=yes$) {
>              set $stripped_params = $1;
>              rewrite ^(.*)$ $1$stripped_params permanent;

-              rewrite ^(.*)$ $1$stripped_params permanent;
+              rewrite ^(.*)$ $1$stripped_params? permanent;

to omit original arguments.
Maxim Dounin (Guest)
on 2009-03-03 10:27
(Received via mailing list)
Hello!

On Tue, Mar 03, 2009 at 12:08:11AM -0800, Neil Sheth wrote:

> I'm trying something like the following, without luck:
>
>          if ($args ~* ^(.*)(&?)extra=yes$) {
>              set $stripped_params = $1;
>              rewrite ^(.*)$ $1$stripped_params permanent;

-              rewrite ^(.*)$ $1$stripped_params permanent;
+              rewrite ^(.*)$ $1?$stripped_params? permanent;

>          }
>
> Any thoughts?  I'm running 0.6.31.  Thanks!

1. Missing '?' to separate uri path and arguments.  Note that $args
doesn't contain leading '?'.

2. Without trailing '?' nginx will append original arguments to
resulting rewrite uri.

Maxim Dounin
Neil Sheth (Guest)
on 2009-03-03 12:21
(Received via mailing list)
Hmmm, I'm getting:

# nginx -t -c /etc/nginx/nginx.conf
2009/03/03 05:05:07 [emerg] 10550#0: invalid number of arguments in
"set" directive in /etc/nginx/nginx.conf:170
2009/03/03 05:05:07 [emerg] 10550#0: the configuration file
/etc/nginx/nginx.conf test failed

Not sure what the error is here.  Also, not sure if this rule belongs
at the server level, or within my "location / " section . . .
Maxim Dounin (Guest)
on 2009-03-03 13:09
(Received via mailing list)
Hello!

On Tue, Mar 03, 2009 at 03:08:06AM -0800, Neil Sheth wrote:

> Hmmm, I'm getting:
>
> # nginx -t -c /etc/nginx/nginx.conf
> 2009/03/03 05:05:07 [emerg] 10550#0: invalid number of arguments in
> "set" directive in /etc/nginx/nginx.conf:170
> 2009/03/03 05:05:07 [emerg] 10550#0: the configuration file
> /etc/nginx/nginx.conf test failed
>
> Not sure what the error is here.  Also, not sure if this rule belongs
> at the server level, or within my "location / " section . . .

[...]

> >> š š š š šif ($args ~* ^(.*)(&?)extra=yes$) {
> >> š š š š š š šset $stripped_params = $1;

-                 set $stripped_params = $1;
+                 set $stripped_params $1;

Maxim Dounin
Neil Sheth (Guest)
on 2009-03-04 00:04
(Received via mailing list)
Ok, that seemed to mostly do what I wanted, thanks!

A couple things -

 I want to avoid writing the trailing '?' if there is no leftover
parameters to pass through:
www.mysite.com/test.php?extra=yes goes to www.mysite.com/test.php?
instead of www.mysite.com/test.php

Similarly, with multiple parameters, it seems to leave the final '&' on
the end.

Do I need to create special rules for each of these?  Or, can I add
logic to test the content of $stripped_params and set the rewrite
accordingly?

Also, I'm not clear, where is it most appropriate to place this rule -
under server, or under location / ?


2009/3/3 Maxim Dounin <mdounin@mdounin.ru>:
This topic is locked and can not be replied to.