Build your Own Reverse proxy
Microsoft’s Yarp is an open-source project for building your own reverse proxy. YARP is different from other proxies because it is built as a library that gives you the core proxy functionality. You can then change it by adding or removing modules. YARP is available as a NuGet package and code snippets.
Before delving into specifics, let’s define what a reverse proxy is and its utility.
Consider a microservice project with N services deployed. This setup works great in theory, but in practice, clients calling these services will struggle if each one has a unique URL. Step in the Reverse Proxy
To put it simply,
A reverse proxy is a server that sits in front of other web servers and relays requests from clients (such as web browsers) to the underlying web servers.
In most cases, organizations use reverse proxies to boost their network’s safety, efficiency, and dependability. First, we need to define a proxy server to understand how a reverse proxy functions and the advantages it can offer.
You're not alone if you’re like me and you’re wondering how it’s different from a load balancer. Read this article:
What is the difference between Load Balancer and Reverse Proxy?
While the net result (distributing requests between servers) is the same between various load balancers and reverse…
TLDR: A load balancer distributes traffic to servers in the L4 of the TCP stack. In L7, a reverse proxy distributes traffic using HTTP.
Enough of the theory; let us now build the reverse proxy. Let’s begin with the bare minimum dotnet6 project template.
Add a NuGet package for Yarp reverse proxy.
Product Versions .NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos…
All that remains is to add a service and map the request pipeline to complete the work.
As previously stated, the Reverse Proxy is a mapper between a downstream system (Mobile/Desktop) and a service; thus, using the same analogy, we must define a mapping configuration that provides a simple rule such as
When a downstream system calls the API, the URL (/api/google) should point to an internal service [www.google.co.in].
You can define the configuration using the appsettings.json
There are two items in the configuration.
1. The Routes Section contains the downstream URL, in this case, /API/google. If the URL matches, proceed to Cluster1.
2. Clusters contain the upstream URL to match, so the destination address is https://google.co.in in this case.
Well, there is a transform that can be used to transform the request, as I don't want to pass the path.
https://localhost:7054 — Google Chrome — 18 October 2022 — Watch Video
We built an extensible reverse proxy in 10 minutes that is also highly scalable.
Happy coding !!!