Ocelot: Redirects browser to downstreamhost url

Created on 7 Jan 2019  路  4Comments  路  Source: ThreeMammals/Ocelot

I am setting up a gateway for my microservice using docker, my image names are pim.services.. And I could not figure out why my browser redirects me to the downstream url that I have in my config.

It takes me to "https://pim.services.patient.api:44302/api/routes/list" when I try to load https://localhost:44300/p/routes/list? What am I missing here?

in my Program.cs I have

            var host = CustomWebHost
                .CreateBuilder<Startup>(args)
                .ConfigureAppConfiguration((webHost, configBuilder) => {
                    configBuilder.AddJsonFile("ocelot.json");
                })
                .Build();

            host.Run();

and in my startup I have

        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            app.UseOcelot().Wait();
        }

and

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddHsts(options =>
            {
                options.Preload = true;
                options.IncludeSubDomains = true;
                // The default HSTS value is 30 days.You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                options.MaxAge = TimeSpan.FromDays(1);
            });

            services.AddOcelot(Configuration);
        }

Controller

[Route("api/[controller]")]
    public class RoutesController : Controller
    {
        private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;

        public RoutesController(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
        {
            this._actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
        }

        [HttpGet("list")]
        public IActionResult Index()
        {
            var routes = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Select(x => new
            {
                Action = x.RouteValues["Action"],
                Controller = x.RouteValues["Controller"],
                RouteName = x.AttributeRouteInfo?.Name,
                RouteTemplate = x.AttributeRouteInfo?.Template,
                Contraint = x.ActionConstraints
            }).ToList();

            return Ok(routes);
        }
    }

ocelot.json

{
    "ReRoutes": [
        {
            "DownstreamPathTemplate": "/api/{catch}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "pim.services.patient.api",
                    "Port": 80
                }
            ],
            "UpstreamPathTemplate": "/p/{catch}",
            "UpstreamHttpMethod": [ "Get" ]
        },
        {
            "DownstreamPathTemplate": "/api/{catch}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "pim.services.is4host",
                    "Port": 80
                }
            ],
            "UpstreamPathTemplate": "/s/{catch}",
            "UpstreamHttpMethod": [ "Get" ]
        }
    ],
    "GlobalConfiguration": {
        "BaseUrl": "https://localhost:44300",
        "RequestIdKey": "OcRequestId"
    }
}
question

Most helpful comment

You are using app.UseHttpsRedirection(); on the service behind, but you have downstream scheme set to HTTP, so the service is trying to redirect the HTTP request to HTTPS, but the location header is not being rewritten on the redirect.

All 4 comments

You are using app.UseHttpsRedirection(); on the service behind, but you have downstream scheme set to HTTP, so the service is trying to redirect the HTTP request to HTTPS, but the location header is not being rewritten on the redirect.

Closing this for now, reopen if my answer did not address the issue.

@philproctor how to rewritten the location header on the redirect

@philproctor even if I comment the app.UseHttpsRedirection() the app still redirects to https instead of http. Can you help me further on how to stop this auto redirection?

Was this page helpful?
0 / 5 - 0 ratings