5

I'm trying to figure out how to configure a reverse proxy in nginx when the endpoint is in a root context:

http://frontend.com/mylink proxy forwards to http://10.0.0.2:8000/

Unfortunately I can't change the context of the application at http://10.0.0.2:8000, so I'm trying to figure out a workaround in nginx for this issue. The following configuration usually works fine when there is a context, but doesn't work in the situation above:

location /mylink {
    proxy_pass     http://10.0.0.2:8000;
    proxy_redirect http://10.0.0.2:8000 /mylink;
    port_in_redirect off;
}

Any ideas what I'm missing here?

tftd
  • 1,498
  • 7
  • 25
  • 40

2 Answers2

3

I think if you change it to:

location /mylink {
   proxy_pass     http://10.0.0.2:8000/;
   proxy_redirect http://10.0.0.2:8000 /mylink;
   port_in_redirect off;
}

Notice the / on the proxy_pass line, this causes the matched part of your location not to be sent to the server.

For reference, see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

Joel
  • 31
  • 2
3

As your location and proxy_pass don't have a trailing slash the normalized URI won't see the /mylink part cut properly nor the proxy_redirect default behaviour append it. Simply use this :

location /mylink/ {
    proxy_pass http://10.0.0.2:8000/;
}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50