Need help on Nginx proxy_pass.

From outside Nginx URL will be hit like this: http://some-IP:8080/v2/platform/general/activity/plan?.....

my downstream service looks like this: http://another-IP:8080/activity/plan?...

I want to get rid of

/v2/platform/general

from original public url and call my downstream service like above.

In Nginx, how do I redirect public access URL to downstream service?

I tried this:

location /v2/platform/general/ {
  rewrite ^/(.*) /$1 break;
  proxy_redirect off;
  proxy_pass http://another-IP:8080;
  proxy_set_header Host $host;

But it didn't work, any help appreciated.

asked Jan 22, 2020 at 3:32

bluelabel's user avatar

bluelabelbluelabel

2,1147 gold badges31 silver badges46 bronze badges

proxy_pass and proxy_redirect have totally different functions. The proxy_redirect directive is only involved with changing the Location response header in a 3xx status message. See the NGINX proxy_redirect docs for details.

Your rewrite statement does nothing other than prevent further modification of the URI. This line needs to be deleted otherwise it will inhibit proxy_pass from mapping the URI. See below.

The proxy_pass directive can map the URI (e.g. from /v2/platform/general/foo to /foo) by appending a URI value to the proxy_pass value, which works in conjunction with the location value. See this document for details.

For example:

location /v2/platform/general/ {
    ...
    proxy_pass http://another-IP:8080/;
}

You may need to set the Host header only if your upstream server does not respond correctly to the value another-IP:8080.

You may need to add one or more proxy_redirect statements if your upstream server generates 3xx status responses with an incorrect value for the Location header value.

mikemaccana's user avatar

mikemaccana

124k110 gold badges432 silver badges533 bronze badges

answered Jan 22, 2020 at 9:08

Richard Smith's user avatar

Richard SmithRichard Smith

49.9k7 gold badges95 silver badges95 bronze badges

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.