reverse-proxy icon indicating copy to clipboard operation
reverse-proxy copied to clipboard

Can I preserve original subdomain on request?

Open dkaczor97 opened this issue 2 years ago • 2 comments

Is it possible to configure YARP so when it receives request with subdomain, it preserves the subdomain and passes the request to another address adding the subdomain?

For example: tenant1.proxy.com/import/something would be passed to tenant1.api.com/something/import but the tenant1 name is dynamic and cannot be set in configuration.

I have tried given configuration without success:

"ReverseProxy": {
  "Routes": {
    "Import": {
      "ClusterId": "Import",
      "Match": {
        "Host": "*.proxy.com",
        "Path": "/import/something"
      },
      "Transforms": [
        {
          "PathSet": "/something/import"
        }
      ]
    }
  },
    "Clusters": {
      "Import": {
        "Destinations": {
          "Import": {
            "Address": "*.api.com"
          }
        }
      }
    }

dkaczor97 avatar Mar 20 '24 14:03 dkaczor97

I don't believe you can express destination host transformations like that with the built-in config transforms. But you can do it from code, e.g. something like

builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
    .AddTransforms(builder =>
    {
        builder.AddRequestTransform(context =>
        {
            var host = context.HttpContext.Request.Host.Value;
            var tenant = host.AsSpan(0, host.IndexOf('.'));

            context.ProxyRequest.RequestUri = RequestUtilities.MakeDestinationAddress(
                $"https://{tenant}.api.com", context.Path, context.Query.QueryString);

            return ValueTask.CompletedTask;
        });
    });

You can also take a look at direct forwarding if that would fit better in your scenario.

MihaZupan avatar Mar 20 '24 15:03 MihaZupan

Triage: Seems to be answered, closing. Let us know if we missed anything.

karelz avatar Mar 26 '24 18:03 karelz