Friday, September 2, 2022

[FIXED] how to get url parameter and pass to proxy_pass using nginx

Issue

I need to get the parameter from an URL, for example, abc=MY_STRING:

https://my-address/test?abc=MY_STRING

And at the reverse proxy (my-address), is configured like this:

location /test?(.*) {
  proxy_pass http://local-server:1234/test?$args
}

but it is not working.

I tried another configuration:

location /test?(.*) {
  proxy_pass http://local-server:1234/test?$1
}

but not worked too.


Solution

You cannot match the query string part of the URI with a location or rewrite statement, as it is not part of the normalized URI.

But you don't need to. The URI (complete with query string) will be passed upstream unless you redirect it using a rewrite or try_files statement.

For example:

location /test {
    proxy_pass http://localhost:1234;
}

The URI /test?abc=MY_STRING will match the location and be passed to localhost:1234 exactly the same. See this document for more.



Answered By - Richard Smith
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.