Nginx: Reverse Proxy and Load Balancing

Reverse Proxy Fundamentals

A reverse proxy is a server that sits between client requests and backend servers, forwarding traffic to the appropriate destination. Nginx is widely used as a reverse proxy due to its efficiency, flexibility, and ease of configuration.

Why Use Nginx as a Reverse Proxy?

  • Improves security by hiding backend server details.
  • Enables SSL termination to offload encryption/decryption.
  • Enhances load distribution and failover support.
  • Provides caching and compression for performance gains.

Basic Nginx Reverse Proxy Configuration

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Testing Reverse Proxy Setup

curl -I http://example.com

Load Balancing Strategies

Nginx supports multiple load balancing algorithms to distribute client requests across multiple backend servers efficiently.

Overview of Load Balancing Methods

  • Round-robin (default): Evenly distributes requests across all servers.
  • Least connections: Sends traffic to the server with the fewest active connections.
  • IP hash: Routes each user’s requests to the same backend server based on their IP address.

Round-Robin Load Balancing

This is the default method, distributing traffic evenly across all upstream servers.

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}
server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}

Least Connections Load Balancing

This method directs new requests to the server with the fewest active connections, reducing overload on busy servers.

upstream backend {
    least_conn;
    server backend1.example.com;
    server backend2.example.com;
}

IP Hash Load Balancing

This method ensures a user consistently connects to the same backend server by hashing their IP address.

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

Upstream Server Configuration

To enhance reliability, you can configure upstream health checks to detect failed servers and reroute traffic accordingly.

upstream backend {
    server backend1.example.com max_fails=3 fail_timeout=30s;
    server backend2.example.com;
}

Monitoring Upstream Server Status

curl -I http://example.com

Session Persistence and Load Balancing

In some cases, applications require session persistence, meaning a user should stay connected to the same backend server for a consistent experience.

Using Sticky Sessions

upstream backend {
    sticky;
    server backend1.example.com;
    server backend2.example.com;
}

Hands-On Exercise

  • Configure Nginx as a reverse proxy for a backend application.
  • Implement round-robin and least connections load balancing.
  • Set up upstream server health checks to improve availability.
  • Enable session persistence for user requests.

By mastering reverse proxying and load balancing, you’ll be able to improve application performance, scale efficiently, and ensure high availability in production environments.