Can I preserve original subdomain on request?
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"
}
}
}
}
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.
Triage: Seems to be answered, closing. Let us know if we missed anything.