0

I am trying to set up a reverse proxy for requesting rest api, let's say for instance that i am making a request like this:

127.0.0.1/v1/resources/get/list/23948

The expected request on target host should be like this:

http://api.example.com/v1/resources/get/list/23948?key=skdma239mfmd0idfm9844

You can see in the expected request there is a query string, the reason for this is to protect the values from the client so the sensitive data is not leaked.

I have tried location definitions like this:

location /v1/resources/get/ {
    proxy_pass http://api.example.com$uri?key=sdkmfg234msdkmad9898
}

but for some reason NGINX responds with 500.

Is it possible to add query strings in a proxy_pass? or is it possible to add the query string to the reverse proxy so this data is protected?

NOTE: i know there is a question in stackoverflow that looks similar to this one, but none of the answers in that question helps with my problem. I have edited the code on how i attempted it last time and nginx responds 502 status

Oscar Reyes
  • 4,223
  • 8
  • 41
  • 75
  • 1
    Possible duplicate of [How can query string parameters be forwarded through a proxy\_pass with nginx?](https://stackoverflow.com/questions/8130692/how-can-query-string-parameters-be-forwarded-through-a-proxy-pass-with-nginx) – Neil VanLandingham Jul 20 '19 at 00:01
  • It's possibly because of the extra `/` between the `.com` and the `$uri`. And don't forget the trailing `;`. Try: `proxy_pass http://api.example.com$uri?key=sdkmfg234msdkmad9898;` – Richard Smith Jul 20 '19 at 07:01
  • @Weft none of the answers given in that question helped me. – Oscar Reyes Jul 21 '19 at 01:04
  • @RichardSmith i tried what you mentioned, but nginx returns 502 error; – Oscar Reyes Jul 21 '19 at 01:05
  • The 5xx errors should be accompanied by a description of the reason in the error log. – Richard Smith Jul 21 '19 at 06:29
  • @RichardSmith i was checking the error logs and there is one that shows as `*5 no resolver defined to resolve api.example.com, client: 127.0.0.1, server: localhost, request: "GET /v1/resources/get/list/23948 HTTP/1.1", host: "localhost"` – Oscar Reyes Jul 21 '19 at 15:38
  • You need to add a [`resolver`](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver) statement. – Richard Smith Jul 21 '19 at 15:41
  • @RichardSmith does it matter what resolver address to use?.. the address being used in the reverse proxy is also handled by the company, but i don't know if should get a specific address for this – Oscar Reyes Jul 21 '19 at 15:56
  • Any DNS server that’s available to the Nginx service, that can resolve `api.example.com`. – Richard Smith Jul 21 '19 at 15:59
  • @RichardSmith i've used google's 8.8.8.8 address resolver for the sake of testing and it works, though when i access the same address in postman like the request done in the browser, nginx returns 404, why is that? – Oscar Reyes Jul 21 '19 at 16:05

1 Answers1

0

After some deep reading on nginx documentation and with the help of @RichardSmith i was able to successfully make the request with proxy_pass using resolver.

The problem is that when using proxy_pass with a domain, the built-in resolver of nginx will try to resolve by itself to see if the DNS is cached, since there was no DNS resolver defined, nginx fails to complete the request.

my location definition ended up like this:

location /v1/resources/get/ {
    resolver 8.8.8.8;
    proxy_pass http://api.example.com$uri?key=sdkmfg234msdkmad9898
}

since this was for the sake of testing, i used google's default resolver which is 8.8.8.8

Oscar Reyes
  • 4,223
  • 8
  • 41
  • 75