Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 8

Reverse Proxy Routing Explained

Recommended resources

Welcome back to our module on Core Backend Components. In our previous lesson, we established the crucial difference between web servers, which are masters of static content, and application servers, the engines for dynamic business logic. We concluded by introducing the standard hybrid architecture where a web server often plays the role of a reverse proxy, acting as the single front door to the entire system.

This lesson zooms in on that critical role. We'll move beyond simply knowing a reverse proxy forwards traffic and explore the intelligent decision-making process it undertakes for every single request. Your goal for this lesson is to understand precisely how a reverse proxy routes incoming requests to the appropriate backend services. For you as a QA professional, mastering this concept is essential for diagnosing routing issues, designing robust integration tests, and understanding performance at the system's edge.

The Reverse Proxy: A Smart Gateway

At its core, a reverse proxy is a server that sits between clients (like web browsers or mobile apps) and your backend services. It accepts all incoming requests and forwards them on the client's behalf. From the client's perspective, it appears to be the actual origin server. This intermediary position allows it to perform a host of critical functions that enhance the security, performance, and reliability of the entire system.

The following video from ByteByteGo provides an excellent high-level overview of why systems use reverse proxies and the key benefits they provide.

Proxy vs Reverse Proxy (Real-world Examples)

This video, "Proxy vs Reverse Proxy (Real-world Examples)," clearly explains the purpose of a reverse proxy in a modern architecture.

Please watch the segment on reverse proxies, which covers their main benefits: the definition, protection, load balancing, caching, and SSL termination.

As the video illustrates, the reverse proxy is far from a simple message passer. It's a highly sophisticated component responsible for managing the connection, processing HTTP requests, and much more.

For a more structured breakdown of these responsibilities, let's turn to an article from USENIX written by an engineer with deep experience operating large-scale proxy systems.

Design Tradeoffs at the Edge | USENIX

This article details the core functions of a reverse proxy from an operational perspective.

Read the list of high-level functions that a reverse proxy performs, starting from the section Core Functions. As you read, consider how each function could be a point of failure or a target for testing. For instance, how would you test if Authentication and authorization at the proxy level is working correctly?


The Three-Step Routing Decision

Our main focus is on how the proxy decides where to send a request. This isn't a single action but a sequence of decisions. Let's break it down into three logical steps:

  1. Identify the Target Service: Which type of service should handle this request?
  2. Choose a Specific Instance: Which specific server running that service should get this request?
  3. Verify the Instance's Health: Is that chosen server actually able to handle the request right now?

Step 1: Identifying the Target Service (Layer 7 Routing)

In a modern microservices architecture, different functionalities are handled by different services. For example, user profile management is handled by a user-service, while payment processing is done by a payment-service. The reverse proxy acts as a traffic director, inspecting incoming requests to determine which service is the appropriate destination.

This is often called Layer 7 Routing or Application Layer Routing because the proxy makes decisions based on application-level data, most commonly the request's URL path or its hostname.

  • Path-Based Routing: A request to https://api.example.com/users/123 would be routed to the user-service, while a request to https://api.example.com/orders/456 would go to the order-service.
  • Host-Based Routing: A request to api.example.com might go to your main backend, while a request to blog.example.com is routed to a separate content management system.

The following diagram shows a typical cloud architecture where an Application Load Balancer (ALB) and NGINX instances work together to route requests based on the URL.

A request for `/service-a/my-service` is routed through DNS and an Application Load Balancer to a pool of NGINX servers. These NGINX servers then forward the request to the correct backend, `Service A`, based on the request path.

This concept is best understood by looking at a real configuration. NGINX is one of the most popular reverse proxies in the world. Let's see how it implements path-based routing.

How to Configure Nginx as a Reverse Proxy for Microservices

This guide from OneUptime provides practical NGINX configuration examples.

Focus on the section Path-Based Routing. Notice how the location blocks are used to match URL prefixes (/users/, /orders/) and the proxy_pass directive specifies the group of backend servers (user_service, order_service) to forward the request to.

Step 2: Choosing a Specific Instance (Load Balancing)

Once the proxy identifies the correct service group (e.g., user_service), it needs to pick one specific server instance from that group. This is load balancing. Its purpose is to distribute traffic to prevent any single server from being overwhelmed, thereby improving performance and ensuring high availability.

Several algorithms can be used for this decision:

  • Round Robin: The default method. Requests are sent to servers in a simple rotating sequence. Server 1, Server 2, Server 3, Server 1, etc. It's simple but can be inefficient if requests have different complexities.
  • Least Connections: The proxy sends the next request to the server that currently has the fewest active connections. This is more intelligent than Round Robin, especially when some requests take longer to process than others.
  • IP Hash: A hash of the client's IP address is used to determine which server receives the request. This ensures that requests from the same user are "sticky" and always go to the same server. This can be useful for stateful applications, but is often avoided in modern, stateless designs which we will cover in a future lesson.

The guide we just looked at provides clear examples of how these are configured.

How to Configure Nginx as a Reverse Proxy for Microservices

This guide continues to be a useful reference for practical configuration.

Now, review the sections on Upstream Backends and Load Balancing Methods. Pay attention to the upstream block, which defines the pool of servers, and the directives like least_conn and ip_hash that change the routing strategy.

Step 3: Verifying Instance Health (Health Checks)

Sending traffic to a dead or unresponsive server is worse than not sending it at all. To prevent this, reverse proxies constantly monitor the health of backend instances.

There are two main approaches:

  • Active Health Checks: The proxy periodically sends its own simple, lightweight request (e.g., to a /health endpoint) to each backend server. If a server fails to respond correctly, it is temporarily marked as "unhealthy" and removed from the load balancing rotation.
  • Passive Health Checks: The proxy monitors the real traffic it's forwarding. If it encounters a certain number of consecutive connection errors or timeouts when trying to contact a server, it will mark that server as "unhealthy."

This mechanism is fundamental to building fault-tolerant systems. As a QA engineer, testing these failover scenarios is critical. What happens when you manually shut down one of the backend instances? Does the proxy detect it and seamlessly redirect traffic to the healthy instances?

The OneUptime guide shows a simple configuration for passive health checks. The USENIX article goes deeper into the real-world complexities.

Design Tradeoffs at the Edge | USENIX

This expert article provides nuanced insights into the challenges of discovery and health checking.

First, read the section that discusses the practical challenges: Challenges in Practice. Then, review the practical lessons learned in the section What We Learned. This gives you a sense of the trade-offs involved—for example, how aggressive health checks can sometimes do more harm than good.


The Full Request Lifecycle Inside a Proxy

Now, let's put it all together and trace the path of a single request through the internal stages of a powerful reverse proxy like NGINX.

This diagram shows the detailed processing phases a request undergoes inside NGINX, from initial header reading to interacting with upstream services and filtering the final response.

Let's walk through this flow:

  1. Read Request Headers: The proxy receives the request from the client and performs initial processing, including terminating the TLS connection (decrypting the request).
  2. Identify Configuration Block: The proxy uses the hostname and URL path to find the matching server and location blocks in its configuration—this is the path-based routing decision we discussed.
  3. Apply Rate Limits: It checks if the client has exceeded its allowed number of requests in a given time period. This is a crucial defense against abuse.
  4. Perform Authentication: It might inspect for an API key or a JWT token and validate it, potentially by calling an external authentication service.
  5. Generate Content (Proxy to Upstream): This is the core routing step.
    • It looks up the upstream group defined in the matched location block.
    • It consults its list of healthy instances for that group.
    • It applies the configured load balancing algorithm (e.g., least connections) to select a single instance.
    • It forwards (proxy_pass) the request to that chosen backend instance.
  6. Upstream Services: The backend service processes the request and sends a response back to the proxy.
  7. Response Filters: The proxy receives the response. Before sending it to the client, it might perform actions like compressing the body with gzip, or adding/removing headers (e.g., adding CORS headers for security).
  8. Log: Finally, the details of the request and response are written to an access log for monitoring and debugging.

This detailed flow highlights numerous touchpoints for quality assurance. Each stage—rate limiting, authentication, header manipulation, failover logic—is a distinct area that requires targeted testing to ensure the system is both functional and resilient.


Conclusion

In this lesson, we demystified the role of the reverse proxy, transforming it from a simple black box that "forwards traffic" into an intelligent gateway that performs a sophisticated routing dance for every request.

Here are your key takeaways:

  • A reverse proxy acts as a single entry point for all traffic, providing load balancing, security, and caching.
  • Layer 7 routing allows the proxy to make intelligent decisions based on application data like the URL path or hostname.
  • The routing process involves three key steps: identifying the service, load balancing across instances using algorithms like Round Robin or Least Connections, and ensuring reliability through health checks.
  • The internal processing lifecycle of a proxy involves multiple stages, from authentication and rate limiting to response modification, each presenting opportunities for testing and quality validation.

You now understand how a request, having arrived at your system's front door, is intelligently directed to a specific application instance. In our next lesson, we will follow the request inside that instance and explore how a backend application routes it through its own internal pipeline of validation, business logic, and response generation.