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"
}
}