0

I'm configuring nginx as reverse proxy.

I need to change (rewrite?) the URLs, example: when the request (to nginx Reverse Proxy) is "http://example.com/test/?username=test1;password=passwdtest1" it will must "modified" to the main server as "http://example.com/test/?username=production;password=passwdproduction1".

Consider that in the original request the fields "username=test1;password=passwdtest1" are not always the same (they changes), instead the "modified" to the main server are always the same.

Others example to be more clear:

"/test/?username=test1;password=passwdtest1" -> "/test/?username=production;password=passwdproduction1"

"/test/?username=test1876;password=somepasswd" -> "/test/?username=production;password=passwdproduction1"

"/test/?username=somevalues;password=somepasswdvalue" -> "/test/?username=production;password=passwdproduction1"

So, independently to what are the values of "?username=somevalues;password=somepasswdvalue" it should always become "?username=production;password=passwdproduction1".

Thanks for your help!

Fabio
  • 3
  • 3
  • UPDATE: I solve in this way: `location ~ /test/ { if ($args ~ "username=(.+);password=(.+)") { rewrite ^.*$ "/test/?username=production;password=passwdproduction1" break; }` – Fabio May 05 '17 at 12:54

1 Answers1

0

A little late on the answer but this should work for you:

location ~* /test/? {
    if ($arg_username ~ "^$|\s+") { return 404; }
    if ($arg_password ~ "^$|\s+") { return 404; }
    rewrite ^ /test?username=production&password=passwdproduction1? permanent;
}

The code above checks if it is within the example.com/test path. If it is it will check if the user name or the password variable are present and not empty in the query string. In case if any isn't present or is empty it will return a 404 else it will redirect you to the preferred url.

By the way, instead of the semicolon in your example urls I would use an ampersand (&).

Robbert
  • 444
  • 5
  • 13