Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I preserve original subdomain on request? #2445

Closed
dkaczor97 opened this issue Mar 20, 2024 · 2 comments
Closed

Can I preserve original subdomain on request? #2445

dkaczor97 opened this issue Mar 20, 2024 · 2 comments

Comments

@dkaczor97
Copy link

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"
          }
        }
      }
    }
@MihaZupan
Copy link
Member

MihaZupan commented Mar 20, 2024

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.

@karelz
Copy link
Member

karelz commented Mar 26, 2024

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

@karelz karelz closed this as completed Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants