Skip to content
~/writing
.NETInfrastructure

Exploring YARP, the reverse proxy for .NET

/ 4 min read / Ejan Shrestha

Introduction

In modern web development, as applications scale, the need for efficient routing, load balancing, and reverse proxying becomes crucial. A reverse proxy can be a game-changer, helping to manage traffic, improve performance, and add an extra layer of security to your applications. Enter YARP (Yet Another Reverse Proxy), a powerful and flexible reverse proxy library developed by Microsoft specifically for .NET developers. This article explores what YARP is, why you should consider using it, and how to get started with it in your .NET projects.

What is YARP?

YARP is a highly customizable reverse proxy toolkit for building fast and reliable proxies in .NET. Built on top of ASP.NET Core, it leverages the underlying framework's performance and scalability while providing a flexible way to define routing rules and load balancing strategies.

One of the standout features of YARP is its extensibility. Unlike traditional reverse proxies, YARP is designed to be modified and extended by developers. You can customize its behavior using .NET code, making it a perfect fit for scenarios where out-of-the-box solutions might fall short.

Why Use a Reverse Proxy?

Before diving into YARP, it’s essential to understand why you might want to use a reverse proxy in the first place. A reverse proxy sits between clients and your backend servers, forwarding client requests to the appropriate server. This setup offers several benefits:

  1. Load Balancing: Distribute incoming traffic across multiple backend servers, ensuring no single server is overwhelmed, which leads to better performance and reliability.

  2. Security: By routing all traffic through a reverse proxy, you can hide the identity and internal structure of your backend servers. This adds an additional security layer, as clients interact only with the proxy.

  3. Caching: Reverse proxies can cache responses from your backend servers, reducing the load on those servers and improving response times for clients.

  4. Routing: You can direct traffic to different backend services based on URL patterns, headers, or other criteria, allowing you to manage multiple services or microservices with a single proxy.

  5. SSL Termination: Reverse proxies can handle SSL termination, decrypting incoming requests before forwarding them to backend services, reducing the computational load on your servers.

Getting Started with YARP

Now that we understand the benefits of using a reverse proxy, let’s see how you can implement YARP in your .NET applications.

Installation

To start using YARP in your project, you need to add the YARP NuGet package to your .NET project. You can do this via the .NET CLI or by adding the package directly through your project’s package manager.

dotnet add package Microsoft.ReverseProxy
Basic Configuration

YARP is configured primarily through your appsettings.json file, where you define routes and clusters. A route in YARP specifies how requests should be matched and routed to a backend cluster. A cluster represents one or more destinations (servers) where the traffic will be forwarded.

Here’s a basic example of a YARP configuration in appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "users-route": {
        "ClusterId": "users-cluster",
        "Match": {
          "Path": "users-api/{**catch-all}"
        },
        "Transforms": [
          {
            "PathPattern": "{**catch-all}"
          }
        ]
      }
    },
    "Clusters": {
      "users-cluster": {
        "Destinations": {
          "destination1": {
            "Address": "https://localhost:7247/"
          }
        }
      }
    }
  }
}
Understanding the Configuration

Let’s break down the configuration:

  • Routes: Defines how incoming requests should be routed. In the example above, we have a route named users-route that matches any request path starting with users-api/. The ClusterId property links this route to a cluster (users-cluster), which defines the destination servers.

  • Clusters: Specifies one or more destinations (servers) that should receive the requests. The users-cluster cluster in the example has a single destination with an address of https://localhost:7247/.

  • Transforms: YARP supports request transformations, allowing you to modify the request before forwarding it to the backend. In this example, the transform ensures that the matched path is passed along to the destination.

Adding YARP to the Middleware Pipeline

After configuring YARP in appsettings.json, you need to register it in your Startup.cs (or Program.cs in .NET 6 and later) file to make it part of the middleware pipeline.

Here’s how you can do that:

 
namespace Gateway.Api;
 
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
 
        // Add services to the container.
 
        builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();
 
        builder.Services.AddReverseProxy()
            .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
 
        var app = builder.Build();
 
        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
 
        app.UseHttpsRedirection();
 
        app.MapReverseProxy();
 
        app.MapControllers();
 
        app.Run();
    }
}

In this setup:

  • services.AddReverseProxy() registers the YARP services with the dependency injection container.

  • LoadFromConfig tells YARP to load the configuration from the appsettings.json file.

  • app.MapReverseProxy() enable YARP in the middleware pipeline, allowing it to handle incoming requests.

Advanced Scenarios with YARP

While the basic setup will cover many use cases, YARP's true power lies in its extensibility and advanced features.

  • Custom Load Balancing: YARP supports custom load-balancing strategies. You can implement your own strategy by extending YARP’s default behavior.

  • Health Checks: YARP can monitor the health of your destinations and avoid routing traffic to unhealthy servers.

  • Rate Limiting: You can apply rate limiting to control the flow of traffic to your backend services, protecting them from being overwhelmed.

  • Custom Middleware: Since YARP is built on ASP.NET Core, you can leverage custom middleware to add logging, authentication, or any other functionality before or after requests are handled by YARP.

Conclusion

YARP is a robust and flexible reverse proxy solution tailored for .NET developers. Whether you need simple routing, complex load balancing, or advanced transformations, YARP provides the tools you need to build a performant and scalable reverse proxy. By leveraging YARP, you can ensure your applications are ready to handle the demands of modern web traffic, all while staying within the familiar .NET ecosystem.

Whether you’re working on a monolithic application that needs to scale or managing a microservices architecture, YARP offers a compelling solution for reverse proxying that integrates seamlessly with your .NET projects.

So, if you haven’t explored YARP yet, now is the time to dive in and see how it can help simplify and enhance your application’s architecture. Happy coding!

Ejan Shresthaइजन श्रेष्ठ

Senior Software Engineer, backend and .NET modernisation

Senior software engineer in Kathmandu with 4+ years shipping .NET for insurance and enterprise. I own reliability and modernisation for an insurance platform of 50 .NET projects on Kubernetes: structured logging and tracing across old and new services, the plan off end-of-life .NET to .NET 10, and the fix for socket exhaustion across ~390 call sites.

Selected impact

  • ~390Ending socket exhaustion across the platform. The error class disappeared from production logs, and outbound HTTP now has one place for retries and tracing.
  • 1 IDOne trace from gateway to database. One correlation ID now follows a request end to end.
  • 61%From end-of-life .NET to .NET 10. A sequenced roadmap with known risks; the first services already run on .NET 10.
  • v16A public website on Next.js 16. Shipped to production with a tested rollback path.
  • 4A release standard people follow. Releases are predictable enough to schedule.

Experience

  1. Senior Software Engineer, Himalayan Everest Insurance

    Jan 2025 – present

    Kathmandu, hybrid

    • Own backend reliability and modernisation for an insurance platform of 50 .NET projects, deployed as 28 Kubernetes workloads, across accounting, claims, reinsurance and reporting.
    • Leading the upgrade from .NET Core 2.2, 3.1 and 5 to .NET 10, including the IdentityServer4 replacement.
    • Built structured logging and tracing across every service.
    • Upgraded the public website and CMS to Next.js 16, React 19, Strapi 5 and Node 24.
  2. .NET Developer, EKbana Solutions

    Jul 2022 – Nov 2024

    Lalitpur, on-site

    • Designed backend services for enterprise insurance workflows with ASP.NET Core, PostgreSQL, EF Core and Dapper.
    • Built REST APIs and integrations for customer portals, payments and external partners.
    • Ran background workflows on Hangfire with idempotency, retries and failure recovery.
    • Shipped on Docker, Linux, Nginx/Traefik and Kubernetes; worked on IdentityServer, JWT and RBAC.
  3. Dotnet Developer, NBIT

    Jan 2022 – Jun 2022

    Lalitpur

    • Built and maintained .NET Framework applications for client projects.
  4. Data Specialist, CloudFactory

    Nov 2017 – Jun 2022

    Kathmandu

    • Delivered high-accuracy data work for international clients while completing my degree.