> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Nginx Use Cases

> Overview of common NGINX roles—load balancer, reverse and forward proxy, and caching, with explanations, analogies, and concise configuration examples to improve performance, scalability, and security

NGINX is much more than a basic web server. This guide covers its most common roles—load balancer, reverse proxy, forward proxy, and cache—explaining why and how each role is used to improve performance, scalability, and security for web applications. You’ll also find compact configuration examples to try locally.

This article summarizes typical NGINX use cases and explains each with simple analogies and example configurations to make the concepts easier to grasp.

## Load balancing

Think of a busy restaurant: if one waiter handled every table, wait times would grow. Adding waiters and distributing tables improves throughput and the customer experience.

An NGINX load balancer acts like the restaurant manager — it receives incoming requests and distributes them across multiple backend web servers. Common benefits include higher throughput, improved availability, and easier maintenance because you can add or remove backends without downtime in most deployments (NGINX supports graceful reloads). Open-source NGINX uses passive health checks (marking backends down after failures); active health checks and additional monitoring are available with [NGINX Plus](https://www.nginx.com/products/nginx-plus/) or via third-party modules.

Minimal load-balancer config example (open-source):

```nginx theme={null}
upstream backend {
    server web01.example.com;
    server web02.example.com;
    server web03.example.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

![A diagram titled "Load Balancing With Nginx" showing multiple users connecting through a network cloud to an NGINX load balancer, which distributes traffic to several web servers. Green heartbeat lines indicate health checks to each web server.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Introduction/Nginx-Use-Cases/nginx-load-balancing-health-checks-diagram.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=0f8ff941b028d53acaa880d898aea9c9)

## Reverse proxy

A reverse proxy is like handing a package to a post office clerk who forwards it to the appropriate courier on your behalf. The client talks to the clerk (reverse proxy), and the clerk forwards requests to one or more backend servers. Best practice is to keep backend servers on internal-only networks so all external access flows through the reverse proxy.

Reverse proxies commonly provide:

* SSL/TLS termination
* Request routing and path-based forwarding
* Caching and compression
* Authentication and access control
* Centralized logging and rate limiting

Typical reverse-proxy config that terminates TLS and forwards to an internal upstream:

```nginx theme={null}
upstream app_servers {
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/ssl/certs/example.crt;
    ssl_certificate_key /etc/ssl/private/example.key;

    location / {
        proxy_pass http://app_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
```

How a reverse proxy differs from a load balancer

* Load balancer: primarily distributes traffic across multiple backends to improve scale and availability.
* Reverse proxy: primarily intermediates client requests, offering abstraction, security, and centralized controls. A reverse proxy can also perform load balancing, but proxying and centralized management are its main purposes.

## Forward proxy

A forward proxy sits between internal clients and the internet. Using a public library analogy: library computers access the internet through a proxy that filters or blocks certain sites to protect users and can also mask client identities. A forward proxy controls outgoing requests from clients to external servers and is commonly used for caching, filtering, and privacy.

NGINX can be configured as a forward proxy in some scenarios, but this is less common and may require additional modules (for example, handling the CONNECT method for HTTPS) or careful configuration.

Configuring NGINX as a forward proxy can expose internal networks if misconfigured. Production forward-proxy setups typically require authentication, access controls, and careful handling of CONNECT/HTTPS traffic (often via third-party modules).

Basic (limited) forward-proxy example using a resolver and proxy\_pass (note: this does not handle CONNECT by default):

```nginx theme={null}
server {
    listen 3128;

    resolver 8.8.8.8 8.8.4.4 valid=300s;
    location / {
        proxy_pass $scheme://$http_host$request_uri;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

For full forward-proxy functionality (CONNECT method, tunnel support) consider third-party modules such as `ngx_http_proxy_connect_module` or dedicated proxy solutions.

## Caching

Caching is like brewing a large batch of coffee and keeping it ready: when the next customer orders the same drink, you serve it instantly instead of brewing again. A cache stores copies of responses so subsequent requests for the same resource are served immediately without contacting backend servers. This reduces backend load and lowers latency.

NGINX can act as a cache server (for example using `proxy_cache`) and is commonly deployed as a reverse proxy with caching enabled to speed up both dynamic and static content.

Basic caching configuration:

```nginx theme={null}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 80;
    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_pass http://backend;
    }
}
```

![A network diagram showing a reverse proxy (NGINX) between users/network cloud and backend web servers (two Apache servers and a generic web server). The proxy is depicted with caching (HTML and storage icons) and arrows indicating request/response flow.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Introduction/Nginx-Use-Cases/nginx-reverse-proxy-cache-apache-backend.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=cab7cc3b4259c66c67ecc9297b58e8d1)

## Summary

NGINX is a flexible tool that can serve multiple roles depending on your architecture and requirements:

| Role | Primary purpose | Common NGINX features / config keywords |
| - | - | - |
| Load balancer | Distribute traffic and improve availability | `upstream`, `proxy_pass`, health checks (passive), `ip_hash`, `least_conn` |
| Reverse proxy | Centralize access, SSL termination, routing | `proxy_pass`, `ssl_certificate`, `proxy_set_header` |
| Forward proxy | Control outgoing requests, filtering, caching | `proxy_pass` to variable hosts, third-party modules for CONNECT |
| Cache server | Reduce backend load and latency | `proxy_cache_path`, `proxy_cache`, `proxy_cache_valid` |

You can combine these roles in one NGINX instance (for example, reverse proxy + cache + load balancer) to create a high-performance edge that protects and accelerates your origin servers.

Links and References

* [NGINX official documentation](https://nginx.org/en/docs/)
* [NGINX Plus (commercial features)](https://www.nginx.com/products/nginx-plus/)
* [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* Third-party forward proxy module (example): `ngx_http_proxy_connect_module`

You can use these examples as a starting point. Test configurations in a staging environment and adapt health checks, timeouts, caching policies, and access controls to your production needs.

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/9e6f72d7-933d-42dd-a948-ae48d66aecb6/lesson/9868630e-34b1-4e4c-8eb6-cb29859824e8)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.