Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yarp rewriting hardcoded service urls

I don't know, if my question is well formulated, but I try... I'm using yarp as a reverse proxy. Behind the proxy there is a asp.net core service (order-service) on port 5048 e.g.

So the config looks like the following:

"ReverseProxy": {
  "Routes": {
    "orderService": {
      "ClusterId": "orderServiceProps",
      "Match": {
        "Path": "/order-service/{**Remainder}"
      },
      "Transforms": [
        {
          "PathRemovePrefix": "/order-service"
        }
      ]
    }
  },
  "Clusters": {
    "orderServiceProps": {
      "Destinations": {
        "destination1": {
          "Address": "http://localhost:5048"
        }
      }
    }
  }
}

If I type e.g. http://localhost:8080/order-service/api/collection the request gets forwarded to http://localhost:5048/api/collection All well so far. But the order-service also has a simple html gui, which is showing some simple links to some special endpoints.
enter image description here

Now if I click on e.g. the settings link, the service navigates to http://localhost:5048/settings but I want the service navigates to http://localhost:8080/order-service/settings
I don't know if I can reach such a bahavior.
I don't want the user is seeing the 5048 in his browser!
And can I do this without let the service know that it is behind a proxy?

like image 504
GreenEyedAndy Avatar asked Oct 15 '25 14:10

GreenEyedAndy


1 Answers

On the order-service you should configure the X-Forwarded middleware like this:

// in ConfigureServices
services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownNetworks.Clear();
    options.KnownProxies.Clear();
});

// in Configure
app.UseForwardedHeaders();

Check this for more info: Configure ASP.NET Core to work with proxy servers and load balancers

like image 50
Artur Avatar answered Oct 18 '25 21:10

Artur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!