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

Hamed Vaziri 136 Reputation points
2023-09-17T10:43:30.9166667+00:00

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

Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-09-17T16:05:29.33+00:00

    We can’t see you code so we don’t know why it redirects. Typically a web api would not redirect, only return status codes.

    did you change the port number when you changed the protocol?

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Hamed Vaziri 136 Reputation points
    2023-09-18T15:54:49.2433333+00:00

    Yes, It works!!

    First problem : Change port number to 443 when use https for your downstream service!

    Second problem : Use app.UseHttpsRedirection() in your api gateWay project and re-publish to your server. Remember, Clear your browser cache when you call your endPoint!!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.