Search code examples
asp.net-core.net-coreasp.net-core-webapiocelot

ASP.NET Core 7.0 Minimal Web API with Ocelot as API gateway redirects to downstream service


I have an ASP.NET Core 7.0 Minimal Web API with Ocelot as API gateway. I've uploaded it to my production server and use this configuration in ocelot.json config file:

{
    "GlobalConfiguration": {
        "BaseUrl": "https://api.mySite.com"
    },
    "Routes": [
        {
            "UpstreamPathTemplate": "/sync/info",
            "UpstreamHttpMethod": [ "Get" ],
            "DownstreamPathTemplate": "/info",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "sync.mySite.com",
                    "Port": 80
                }
            ]
        }
    ]
}

The problem is that when I send a request to the https://api.mySite.com/sync/info URL, it redirects to https://sync.mySite.com/info in my browser and displays the result.

First question: is this true? How to prevent to redirection when request my API via API gateway (I've also removed the line app.UseHttpsRedirection() from program.cs class of my API gateway project and re-published to production server, but still facing the same result)?

Second question: if I changed downstreamScheme to https, I am facing an http 502 (bad gateway) status code, while when I request downstream service directly via https, it works correctly. Where is the problem and how to solve it?

Thanks in advance


Solution

  • For the first question,

    You should check app.UseHttpsRedirection() in downStream host, not in API Gateway. Because whether you send HTTP request or not, this will turn it into HTTPS.

    Second question,

    As an estimation (because there isn't enough info)

    you changed downStreamScheme to HTTPS, have u changed the port from 80 to 443 ?