# Proxies and load balancing: one front door

Trace forward and reverse proxy paths, route HTTP through Nginx, and understand health checks and layer 4 versus layer 7 balancing.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 9. About 24 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/09-proxies-and-load-balancing

## What you will learn

- Tell a forward proxy from a reverse proxy by whose request it handles
- Read an Nginx proxy_pass rule and trace both connections
- Compare layer 4 and layer 7 balancing
- Explain what a health check can and cannot prove

> **Lab status.** **Verified lab:** two temporary `nginx:alpine` containers ran on a private Docker network. The front Nginx proxied a request to the backend and returned its page. The output is real; the script removes both containers and the network on exit. <br />**Cloud lab:** none. No load balancer or cloud resource was created.

Before proxies, each application often handled public traffic directly. That made certificates, routing, and traffic
spikes every app team's separate problem. A proxy creates one managed entry point and opens a second connection to
the chosen backend. In this lesson, you will follow those two connections, read a real Nginx proxy rule, and compare
load-balancing decisions.

## Forward and reverse proxies

> **Figure.** A client sends a request through a forward proxy toward an origin. A reverse proxy instead sits in front of a service and chooses its backend.

A **forward proxy** acts for clients, often to apply outbound access policy or share an egress point. A **reverse
proxy** acts for a service. Clients connect to it, and it chooses an application server. From the client, both may
look like an ordinary server; the difference is who owns and configures the proxy.

Use a forward proxy when you manage client egress. Use a reverse proxy for a service front door, TLS termination,
or request routing. Neither makes the backend secure by itself. Keep backend ports private and define which headers
the proxy forwards.

## Nginx as a reverse proxy

> **Figure.** A browser sends an HTTP request to Nginx on port 80; Nginx matches a location and opens another HTTP connection to the application on port 3000.

Nginx receives the client connection, matches a `location`, and opens its own upstream connection. The backend
does not see the client's original TCP connection. Forward the original host or client address only when the app
needs it, and trust those headers only from your proxy.

```nginx
server {
  listen 80;
  location / {
    proxy_pass http://backend:80;
  }
}
```

This is the configuration used in the container lab. The real response was:

```text
$ wget -qO- http://127.0.0.1/ (inside the proxy container)
backend response
$ nginx -T (selected directives)
  listen 80;
    proxy_pass http://backend:80;
```

The command inspected the proxy container. Its temporary `backend` name mapped to the backend container's address.
In a production network, use stable service discovery and protect the upstream path.

Before reloading a changed config, validate it:

```text
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
```

This is the real check from the temporary proxy container.

## L4 and L7 load balancing

> **Figure.** A client sends a request to a load balancer. Layer 4 selects using connection details; layer 7 can inspect the HTTP request before choosing a healthy target.

A **layer 4** balancer distributes TCP or UDP connections using addresses and ports. A **layer 7** balancer can
route HTTP using the host, path, or headers. L4 works with many protocols and has less application awareness. L7
can route `/api` and `/images` differently, but must understand that protocol.

Choose the layer based on what the routing decision needs to inspect. A common mistake is assuming an L7 rule
changes DNS or repairs the backend. It only routes the connection it receives.

## Health checks

A health check asks whether a target should receive new traffic. A TCP check proves that a port accepted a
connection. An HTTP check can test a readiness endpoint and status code. The check interval and failure threshold
control how quickly a target leaves or returns to rotation.

Make the check reflect the service's ability to handle work. Do not make it depend on every optional dependency,
or a harmless database hiccup may remove all targets. Do not check only a process that is alive but cannot serve
requests. Nginx's open-source upstream checks are passive: real request failures influence target selection.

## Mission: Trace a request through Nginx

Run the disposable two-container proxy lab. Inspect its proxy_pass rule and confirm that the response came from the backend. The script cleans up its containers and network.

- You need: Docker
- You'll have: backend response from the upstream through Nginx

```sh
bash devops-academy-tools/networking-lab/09-capture.sh
```

Open the script to see the temporary backend, proxy config, and cleanup trap. The request is made to the proxy,
not directly to the backend.

## Try it

- Draw the client-to-proxy and proxy-to-upstream connections for one request.
- Read the Nginx config in the mini mission and find its listener and upstream.
- Change the `location` path in a local copy and validate it with `nginx -t`.
- Remove the temporary network and containers when you finish.

## Quiz

1. A reverse proxy receives a browser request and sends it to an application server. Who configured that proxy?
   - The browser user
   - **The service operator** (answer)
   - The DNS root server
   - The application client
   Why: The service operator places the reverse proxy in front of the application and controls its upstream routing.

2. What can a layer 7 load balancer inspect that a layer 4 balancer generally does not?
   - **HTTP paths and headers** (answer)
   - Ethernet source MAC only
   - The server's source code
   - The client process ID
   Why: Layer 7 understands application requests such as HTTP paths; layer 4 works with transport connections and ports.

3. A target answers TCP but returns an error on the readiness endpoint. What should a useful health check do?
   - Keep routing there because the port is open
   - **Mark it unhealthy if the endpoint signals the app is not ready** (answer)
   - Restart every target
   - Ignore the response code
   Why: A readiness check should reflect whether the service can handle requests, not only whether a socket accepts TCP.

## Resources

- [Nginx proxy module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html) (Nginx Docs)
- [Nginx HTTP load balancing](https://nginx.org/en/docs/http/load_balancing.html) (Nginx Docs)
- [Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html) (AWS Docs)
