2

I have been trying to configure ocelot on IIS with virtual directories and do not understand why the DownstreamHostAndPorts.Host value does not work when the value is localhost. As you can see from the configuration below the gateway sits at http://localhost:8080/ocelot-gateway. When someone sends a POST request to http://localhost:8080/ocelot-gateway/api/service-endpoint, it should send the request to the same server (and same instance of IIS), but different website's virtual directory http://localhost:8082/another-website-virtual-dir/api/service-endpoint. This configuration returns a 404 unless I put the actual server name as the DownstreamHostAndPorts.Host value, then it works. Am I mis-understanding something with the ocelot configuration or IIS? Right now my websites bindings are http, all unassigned and the port (yes, I know "http", but this is just testing).

I can POST to the downstream host & port directly without going through the gateway, so that service is running.

Update So after seeing this message in the event viewer from ocelot:

requestId: 800000a1-0001-ec00-b63f-84710c7967bb, previousRequestId: no previous request id, message: 404 (Not Found) status code, request uri: http://<server ip here>/.

I added an extra binding to the port 80 and 8080 website that bound to the actual ip address of the server in addition to "All Unassigned" (which was already there) thinking I could get rid of the 404. When I run my api request from postman to the port 8080 ocelot gateway, i get a 200, but the response is the index.html from my port 80 website? That confuses me a bit so still looking into it

Host Config in Program.cs

            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    _ = logging.ClearProviders().AddConsole();
                })
                .ConfigureAppConfiguration((hostingContext, builder) =>
                {
                    _ = builder
                        .AddJsonFile("ocelot.json")
                        .AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                        .AddCommandLine(args)
                        .AddEnvironmentVariables(prefix: "AppName_");
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    _ = webBuilder.UseStartup<Startup>();
                });

ocelot.json

{
    "Routes":[{
        "DownstreamPathTemplate": "/another-website-virtual-dir/api/service-endpoint",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
            {
                "Host": "localhost",
                "Port": 8082
            }
        ],
        "UpstreamPathTemplate": "/api/service-endpoint",
        "UpstreamHttpMethod": [ "POST" ],
          "HttpHandlerOptions": {
              "AllowAutoRedirect": true
          },
    }],
    "GlobalConfiguration": {
        "RequestIdKey": "OcRequestId",
        "BaseUrl": "http://localhost:8080/ocelot-gateway"
    }
}
jmzagorski
  • 1,135
  • 18
  • 42
  • I don't understand your config, you what to send a request to another port without config DownstreamHostAndPorts? – AliReza Beigy Aug 16 '21 at 21:01
  • I would like to send a request to another port yes, in my ocelot config that IIS website is 8082 as shown above. I would like to keep the `DownstreamHostAndPorts.Host` as localhost since it is the same server as the ocelot gateway, but that does not work. I have to put the actual server name. For example if my server name was "FooServer", i need to put "FooServer" in `DownstreamHostAndPorts.Host`. Hope that clears it up. – jmzagorski Aug 17 '21 at 00:12
  • I suggest you try to use failed request tracking to view detailed error information. – samwu Aug 17 '21 at 08:23
  • Thanks @samwu, did not know that existed in IIS. I enabled it and the final status code in the trace is 301. Wonder if it has something to do with: https://github.com/ThreeMammals/Ocelot/issues/1113 – jmzagorski Aug 17 '21 at 11:57
  • Can you provide detailed messages in frt? and the error returned in your posting above was 404. why did the status code change to 301 here? how do you configure frt? – samwu Aug 18 '21 at 09:06
  • I found that adding `"HttpHandlerOptions": { "AllowAutoRedirect": true },` to the ocelot.json file changes the request tracking from 301 to 404. I updated my config in the question – jmzagorski Aug 19 '21 at 01:53
  • Set the status code to 404 in the failed request tracking, and then check the error message. – samwu Aug 19 '21 at 09:13
  • I had that request tracking look at 200-500 to make sure it showed all requests to that site. The 404 is from the `AspNetCoreModuleV2`, handler `EXECUTE_REQUEST_HANDLER`, error code `The operation completed successfully (0x0).` I assume that warning directly relates to the Event Viewer warning from the ocelot `HttpRequestMiddleware` since the messages appear at the same time. I added that warning to the update in the question – jmzagorski Aug 19 '21 at 13:25
  • I wonder if it could be something with the web server setup. I can confirm that trying to access a virtual directory where an application runs via `http:///`, does not work, but the given domain name clients use works via `http:///` – jmzagorski Aug 19 '21 at 15:32
  • @samwu I just updated the **Update** in the question. I was able to get a 200, but the response is not what I was expecting. – jmzagorski Aug 19 '21 at 16:34
  • Status code 200 means normal operation, what do you want? – samwu Aug 20 '21 at 07:31
  • The api request I sent was to http://:8080/ocelot-gateway/api/service-endpoint. "ocelot-gateway" is an application within the port 8080 website. The 200 status is coming from the port 80 website and is HTML from the index.html, all of that is wrong. Ocelot should be sending the request to the port 8080 gateway and then that forwards the request to the port 8082 service-endpoint application. The response should be JSON data. – jmzagorski Aug 20 '21 at 12:18
  • Maybe "localhost" as the `DownstreamHost` is not support with ocelot. If it is nothing with IIS, maybe I can open an issue with ocelot in github – jmzagorski Aug 20 '21 at 12:41
  • I suggest you open a case via: https://support.microsoft.com. – samwu Aug 23 '21 at 08:03

0 Answers0