Search code examples
nginxurl-rewritingreverse-proxyjhipsternetflix-zuul

Zuul reverse proxy preserve URL


I have a microservce architecture with several services build using JHipster.

Inside one service i have implemented a zuul route filter.

public class TestZuulFilter extends ZuulFilter {

  @Override
  public String filterType() {
      return "route";
  }

  @Override
  public int filterOrder() {
    return 5;
  }

  @Override
  public boolean shouldFilter() {
      String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI();
      return "/serviceid/reverseproxy".equals(requestUri);
  }

  @Override
  public Object run() {

       // get url from id
       String id = ctx.getRequest().getParameter("id"); 
       Strign url = URLService.getURLFromId(id);

       try 
       {
          RequestContext ctx = RequestContext.getCurrentContext();

          // redirect
          ctx.setRouteHost(new URL(url));

       } catch(MalformedURLException ex) {}

       return null;
  }
}

When a client call my service http://myservice/serviceid/reverseproxy?id=2 zuul redirects (http 302 status) the user to the url with id 2, in this case google.com.

How can i preserve the original request URL from the client ?

The url must remain http://myservice/serviceid/reverseproxy?url=2 instead of http://www.google.com

Thanks in advance.


Solution

  • It seems you misunderstood the concepts of redirection and proxification.

    HTTP redirection means URL change because all the work is done by the client who ends up by making 2 request calls (one to your proxy and one to external service).

    Here what you want is to proxify the original request to an external service (in your example google), it means that your filter should be a client of your external service. This way your original client makes only on request call and has no idea that it is talking to your external service.