Getting started with Microsoft YARP (Yet Another Reverse Proxy)
Posted: (EET/GMT+2)
Building an HTTP reverse proxy from scratch is tedious work, but luckily, Microsoft has created a .NET based library for just that. They call it Microsoft YARP, short for Yet Another Reverse Proxy. It is a simple, extensible and flexible option.
YARP is a library for building reverse proxy functionality on top of ASP.NET Core. It lets you route incoming requests to other backend services, similar to tools like Nginx or IIS, but fully in code.
A common use case is routing traffic to multiple services behind a single endpoint. The configuration can come from code, JSON files, or anything you write. I'm planning to write a custom version that reads configuration from an SQL Server database. Works well!
To get started with YARP, create a new ASP.NET Core application and add the YARP package:
dotnet add package Yarp.ReverseProxy
Then configure routes and clusters in your application's appsettings.json file:
{
"ReverseProxy": {
"Routes": {
"route1": {
"ClusterId": "backend",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"backend": {
"Destinations": {
"destination1": {
"Address": "https://example.com/"
}
}
}
}
}
}
Finally, wire up the reverse proxy in Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();
With this setup, incoming requests are forwarded to the configured backend service.
A few practical notes:
- Configuration can be done in code or from configuration files.
- You can define multiple routes and clusters for more complex setups.
- YARP runs inside your application, so you can combine it with middleware, logging, and authentication.
YARP is especially useful when you want a reverse proxy that is easy to customize using C# instead of managing a separate infrastructure component.