Remove path prefix during proxy_pass

Vignesh VR at
Fri Feb 12 17:34:19 UTC 2016


I am trying to use Nginx as a reverse proxy and want to redirect to
different upstream servers based on the URL. Looking to use the path prefix
to distinguish the upstream servers to be used. But the issue is the path
prefix gets sent to the upstream server which I don't want to.

For eg., this is what I am trying to do:

Nginx Server: nginx  (External IP)

upstream 1: upstream1 (Internal IP)

upstream 2: upstream2 (Internal IP)

Desired outcome:
http://nginx/server1  -------------->   http://upstream1

http://nginx/server2 ----------------> http://upstream2

The problem I am having is that http://nginx/server1 gets translated to
http://upstream1/server1. I am not able to remove the path prefix when the
traffic gets directed to the upstream servers.

I have tried most of the suggestions on the net:

location /server1/ {
  proxy_pass http://upstream1/;

(tried without slash)
location /server1/ {
  proxy_pass http://upstream1;

(tried using regex)
location /server1/ {
  rewrite ^/server1/(.*) /$1 break;
  proxy_pass http://upstream1/;

All of the above results in varied behaviors but none gives the desired
outcome. I am currently using a non-commercial version of nginx.

And please note that I am using IP addresses everywhere and not hostnames.
So name resolution issues anywhere here.

Any guidance / help / pointers will be highly appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list