Ragnar
Ragnar

Reputation: 2690

API Gateway Ocelot does not redirect

Making a micro-service architecture with Ocelot I started to build a test service in a separate solution. Everything is working and I can get the mock response to https://localhost:5101/service/stats/collected.

Then in another solution I'm making a fresh new webapi project. Then I follow the getting started at Ocelot's official website.

Configuring the .json file to use it as a GW I got a 500 from the project if I try to hit https://localhost:5001/api/stats/collected and I cannot figure out why ?

Here the main files for the APIGW :

ocelot.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/service/stats/collected",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5101
        }
      ],
      "UpstreamPathTemplate": "/api/stats/collected"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5001"
  }
}

Program.cs

using System.IO;
using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace APIGateway.Base
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new WebHostBuilder()
                .UseKestrel(
                    options =>
                {
                    options.Listen(IPAddress.Loopback, 5001, listenOptions =>
                    {
                        listenOptions.UseHttps("localhost.pfx", "qwerty123");
                    });
                    options.AddServerHeader = false;
                }
                    )
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", true, true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                        .AddJsonFile("ocelot.json")
                        .AddEnvironmentVariables();
                })
                .ConfigureServices(s => {
                    s.AddOcelot();
                })
                .ConfigureLogging((hostingContext, logging) =>
                {
                    //add your logging
                })
                .UseIISIntegration()
                .Configure(app =>
                {
                    app.UseOcelot().Wait();
                })
                .Build()
                .Run();
        }
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace StatsService.Base
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

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

UPDATE:

I find out that disabling the SSL on each project by commenting the options in the method UseKestrel make my GW works. How can I setup up this to have a secure connexion between my GW and the Service ? Localhost and Prod ?

Upvotes: 1

Views: 12645

Answers (3)

Zia Qammar
Zia Qammar

Reputation: 194

You project is enabled for SSL, you have to disabled the SSL and remove the route with HTTPS in the launchSettings.json in all of your WEB API project. It will work successfully.

Upvotes: 0

Toqeer Yousaf
Toqeer Yousaf

Reputation: 414

I Have to changed ReRoute to Route then my routes redirect correctly.Please Use Route in your ocelot configuration file

Upvotes: 3

Kay Zumbusch
Kay Zumbusch

Reputation: 96

As you are using "localhost" as the hostname, I'd guess that you are using a self signed certificate for localhost. In that case you might have to add

"DangerousAcceptAnyServerCertificateValidator": true

to your ReRoutes configuration (see https://ocelot.readthedocs.io/en/latest/features/configuration.html#ssl-errors).

Other options would be:

  • using a valid certificate (quite hard to achieve for localhost)
  • register the self signed certificate in your user's certificate store as a trustworthy certificate.

Upvotes: 4

Related Questions