ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.38k stars 1.64k forks source link

Does Ocelot support IIS virtual directories (load balancing)? #1615

Closed NunoOlliveira closed 1 year ago

NunoOlliveira commented 2 years ago

Hello, and thank you for this awesome project. I would like to know if there is any chance for supporting IIS virtual directories in a load balancing environment.

If you specify the virtual directory path in the downstream host, it will become an invalid path: Example:

"DownstreamHostAndPorts": [
        {
          "Host": "localhost/MyVirtualDirectory",
          "Port": 80
        },
        {
          "Host": "localhost/MyVirtualDirectory1",
          "Port": 80
        }
      ]

The URL will become http://localhost/MyVirtualDirectory:80 instead of the http://localhost:80/MyVirtualDirectory.

Is this correct or am I missing something in the configuration file.

Best Regards.

chkrishna2001 commented 1 year ago

Have you find any solution for this ? I am having issue with this, port number is getting added after virtual directory and getting

A potentially dangerous Request.Path value was detected from the client (:).

NunoOlliveira commented 1 year ago

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