Repository navigation
Does Ocelot support IIS virtual directories (load balancing)? #1780
|
Hello, and thank you for this awesome project. If you specify the virtual directory path in the downstream host, it will become an invalid path: "DownstreamHostAndPorts": [
{
"Host": "localhost/MyVirtualDirectory",
"Port": 80
},
{
"Host": "localhost/MyVirtualDirectory1",
"Port": 80
}
]The URL will become Is this correct or am I missing something in the configuration file. Best Regards. |
Replies: 6 comments 7 replies
|
Have you find any solution for this ?
|
|
Yes, we have changed 2 methods on the src\Ocelot\Request\Middleware\DownstreamRequest.cs file: public HttpRequestMessage ToHttpRequestMessage()
{
Uri uri = new Uri(new UriBuilder() { Host = Host, Scheme = Scheme }.ToString());
UriBuilder uriBuilder = new UriBuilder
{
Host = uri.Host,
Path = (!string.IsNullOrEmpty(uri.AbsolutePath) && uri.AbsolutePath.Length > 3) ? $"{uri.AbsolutePath[1..^1]}{AbsolutePath}" : AbsolutePath,
Query = RemoveLeadingQuestionMark(Query),
Scheme = Scheme,
Port = Port,
};
_request.RequestUri = uriBuilder.Uri;
_request.Method = new HttpMethod(Method);
return _request;
}and public string ToUri()
{
Uri uri = new Uri(new UriBuilder() { Host = Host, Scheme = Scheme }.ToString());
UriBuilder uriBuilder = new UriBuilder
{
Host = uri.Host,
Path = (!string.IsNullOrEmpty(uri.AbsolutePath) && uri.AbsolutePath.Length > 3) ? $"{uri.AbsolutePath[1..^1]}{AbsolutePath}" : AbsolutePath,
Query = RemoveLeadingQuestionMark(Query),
Scheme = Scheme,
Port = Port,
};
return uriBuilder.Uri.AbsoluteUri;
} |
|
Hi Nuno! I believe you have to define your routes like this: {
"Routes": [
{
"UpstreamHttpMethod": [ "Get" ],
"UpstreamPathTemplate": "/up-path",
"DownstreamPathTemplate": "/MyVirtualDirectory",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 80 }
]
},
{
"UpstreamHttpMethod": [ "Get" ],
"UpstreamPathTemplate": "/up-path1",
"DownstreamPathTemplate": "/MyVirtualDirectory1",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 80 }
]
}
]
}Pretty simple, right? |
|
Thank you very much for your answer. |
|
Hello, Unfortunately it doesn't work for the load balancing option. In your configuration example where should I put the "LoadBalancerOptions" for the two virtual directories? |
|
Thank you for your answer. Best Regards. |

Hi Nuno!
Your routes definition is incorrect!
Ocelot does support virtual IIS directories, because virtual directory is a path after the host & port string.
I believe you have to define your routes like this:
{ "Routes": [ { "UpstreamHttpMethod": [ "Get" ], "UpstreamPathTemplate": "/up-path", "DownstreamPathTemplate": "/MyVirtualDirectory", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 80 } ] }, { "UpstreamHttpMethod": [ "Get" ], "UpstreamPathTemplate": "/up-path1", "DownstreamPathTemplate": "/MyVirtualDirectory1", "DownstreamScheme": "http", "DownstreamHostAnd…