1

I had a proxy server which redirects communications to some api on customer side via https. When I use configuration with set upstream variable (proxy_pass $upstream_endpoint$request_uri;), the DNS resolving for this domain (dynamic changing IP adress) is working well but I get response 403 unauthorized.

When I use configuration without upstream (proxy_pass https://api-test.example.com/api/), point directly to customer domain it works well, I am getting response 200 but DNS resolver is not working anymore..

Nginx config:

location /api-test.example.com/api/ {
            resolver 10.100.10.1 valid=5s;
            set $upstream_endpoint https://api-test.example.com;
            proxy_pass $upstream_endpoint$request_uri;
            #proxy_pass https://api-test.example.com/api/;
            proxy_ssl_name api-test.example.com;
            proxy_ssl_server_name on;
            proxy_set_header Host api-test.example.com;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
PeterH
  • 27
  • 6
  • `$request_uri` is not the same as `/api/` - are you missing a rewrite? – Richard Smith Oct 11 '18 at 08:27
  • Hi Richard Yes $request_uri is the same as /api/ Do you mean it should be as set $upstream_endpoint https://api-test.domain.com/api/; proxy_pass $upstream_endpoint; – PeterH Oct 11 '18 at 08:40
  • No, I mean that the value of `$request_uri` is equal to `/api-test.domain.com/api/` and not `/api/`. – Richard Smith Oct 11 '18 at 08:42
  • its possible issue. How should i fixed it? just remove from proxy_pass $upstream_endpoint$request_uri; Final should be: proxy_pass $upstream_endpoint; ?? – PeterH Oct 11 '18 at 08:46

1 Answers1

0

By adding a URI to the proxy_pass statement, the requested URI is rewritten before passing it upstream. See this docuement for details.

So the URI /api-test.example.com/api/foo is rewritten to /api/foo.

You can achieve the same behaviour with a rewrite...break statement. See this document for details.

location /api-test.example.com/api/ {
    rewrite ^/api-test.example.com(.*)$ $1 break;
    set $upstream_endpoint https://api-test.example.com;
    proxy_pass $upstream_endpoint;
    ...
}
Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
Richard Smith
  • 45,711
  • 6
  • 82
  • 81