0

ı am coding a microservice architectural with gateway in .NET6 web api. My gateway run on 5000 port on local,
a microservice run on 5001 port on local, other microservice run on 5002 port on local i want to make a pagging in microservices and i return response with pagging links like next,previous... When I make a request to microservices on the gateway, the response I get is as follows

"links": {
    "_previous": "https://localhost:5001/api/customers/5/5?page=4&size=5",
    "_next": "https://localhost:5001/api/customers/5/5?page=6&size=5",
    "_first": "https://localhost:5001/api/customers/5/5?page=1&size=5",
    "_last": "https://localhost:5001/api/customers/5/5?page=1207&size=5"
},   

but I want the links to come on the host(localhost:5000) of the gateway like the example below

"_previous": "https://localhost:5000/api/customers/5/5?page=4&size=5"

I looked at sample questions, I did research on the internet, but somehow I could not get a gateway host on microservice. what should i do to ge the host of the gateway at microservices

ı use ocelot for gateway

notcontrol
  • 109
  • 1
  • 8
  • 1
    who is generating this links?, in my opinion the link should not contain the complete path anyways but only relative path "{url}/api/customers/5/5?page=4&size=5" and {url} should be replace with your api gateway path while calling those APIs. this will also help in production when you really deploy your application. – CodingMytra Jun 24 '22 at 09:02
  • so sorry i wrote wrong pagination response. endpoint is `https://localhost:5001/api/Customers?page=5&size=5` and pagging response is `_previous": "https://localhost:5001/api/Customers?page=4&size=5` this line is generating link `string paggingLink = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}";` – notcontrol Jun 24 '22 at 09:15
  • 1
    in my opinion you should generate link `string paggingLink = $"{HttpContext.Request.Scheme}://{url}/{HttpContext.Request.Path}";` like this. and replace {url} when calling any of the link. because even if you able to get gateway host in the service it will not help in production. – CodingMytra Jun 24 '22 at 09:27
  • How the urls are generated? – Chetan Jun 24 '22 at 09:35
  • ı do not understand what you mean @Chetan – notcontrol Jun 24 '22 at 12:46

1 Answers1

0

You need to make your service aware that it runs behind the reverse proxy.

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

// in Configure
app.UseForwardedHeaders();

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

But I'm not sure Ocelot sets X-Frowarded headers when forwarding the requests. And since it was abandoned by the maintainer, the better option would be to replace Ocelot with Yarp. It does add required headers to all requests.

Artur
  • 4,595
  • 25
  • 38