Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX proxy_pass or proxy_redirect

Tags:

nginx

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.

like image 599
bluelabel Avatar asked Jan 22 '20 03:01

bluelabel


People also ask

What is Proxy_redirect off Nginx?

Nginx provides proxy_redirect directive which can be used in http, server, or location context. The syntax is: proxy_redirect redirect replacement. In this example, the proxied server (upstream Apache or Lighttpd) returned line Location: http://server1.cyberciti.biz:8080/app/.

What is proxy_pass Nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

Why use Nginx reverse proxy?

Security and anonymity – By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks.

How do you check if Nginx reverse proxy is working?

To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.


1 Answers

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 this document 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.

like image 84
Richard Smith Avatar answered Sep 21 '22 06:09

Richard Smith