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. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 7 replies
-
Have you find any solution for this ?
|
Beta Was this translation helpful? Give feedback.
-
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;
} |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your answer. |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer. Best Regards. |
Beta Was this translation helpful? Give feedback.
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: