Proxy a fixed upstream service with YARP
A reverse proxy must handle more than copying response bytes. Status codes, headers, streaming and request cancellation all matter. YARP supplies that forwarding machinery within ASP.NET Core.
Configure a known destination rather than accepting an arbitrary target URL from a query parameter. The latter can turn an endpoint into an open proxy or a path to internal services.
Create the proxy
Requires .NET 10 and an authorized local upstream listening on http://localhost:5091/. Create a project:
dotnet new web -n ProxyDemo -f net10.0
cd ProxyDemo
dotnet add package Yarp.ReverseProxy --version 2.3.0
Replace Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();
Add this section to appsettings.json:
{
"ReverseProxy": {
"Routes": {
"api": {
"ClusterId": "backend",
"Match": { "Path": "/api/{**rest}" },
"Transforms": [ { "PathRemovePrefix": "/api" } ]
}
},
"Clusters": {
"backend": {
"Destinations": { "local": { "Address": "http://localhost:5091/" } }
}
}
}
}
Run the proxy on a different port, for example dotnet run --urls http://localhost:5092. If the upstream serves /health, requesting http://localhost:5092/api/health forwards to it and returns its response. The prefix transformation removes /api before forwarding.
Define the production boundary
Apply the application's authentication/authorization policy to the route, use TLS for external traffic, and configure timeouts and destination trust. Only forward identity or sensitive headers under a deliberate policy. YARP does not make an exposed backend secure by itself. Keep destinations in managed configuration, and verify failure and cancellation behavior as well as successful responses.
Official references

Author: Herman Schoenfeld