Mastering Nginx: From Reverse Proxy to High‑Availability Load Balancing

This guide explains Nginx’s core concepts—including reverse and forward proxy, load‑balancing strategies, static‑dynamic separation, essential commands, configuration file structure, practical reverse‑proxy setups, load‑balancing implementations, and high‑availability clustering with keepalived—providing a complete roadmap for building robust backend services.

Programmer DD
Programmer DD
Programmer DD
Mastering Nginx: From Reverse Proxy to High‑Availability Load Balancing

1. Nginx Overview

Nginx is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency, capable of handling up to 50,000 simultaneous connections.

1.1 Forward and Reverse Proxy

Forward proxy : Clients in a LAN must access the Internet through a proxy server.

Reverse proxy : Clients send requests to a proxy server without configuration; the proxy forwards them to backend servers, hiding the real server IP.

1.2 Load Balancing

When request volume grows, a single server cannot meet demand; adding more servers and distributing requests across them achieves load balancing.

Example: 15 requests are evenly split among three servers, each handling five requests.

1.3 Static‑Dynamic Separation

Separating static and dynamic content speeds up site parsing and reduces load on a single server.

2. Common Nginx Commands

./nginx -v

Check version. ./nginx Start Nginx. ./nginx -s stop Stop Nginx (alternative: ./nginx -s quit). ./nginx -s reload Reload configuration.

3. Nginx Configuration File

The configuration consists of three blocks:

main (global) block : Settings that affect the whole server, including worker processes and connections.

events block : Network‑related settings such as connection handling.

http block : Defines virtual hosts, reverse proxy, load balancing, etc.

location directive syntax : location [ = | ~ | ~* | ^~ ] url { } Modifiers:

=: exact match, stop searching.

~: case‑sensitive regex.

~*: case‑insensitive regex.

^~: highest‑priority prefix match, stop further regex checks.

3.1 Reverse Proxy Example

Goal: Access www.123.com and have Nginx forward the request to a Tomcat instance.

Configure Tomcat on port 8080, then set up Nginx to listen on port 80 and proxy to http://localhost:8080. After reloading Nginx, browsing www.123.com displays the Tomcat page.

3.2 Second Reverse Proxy Scenario

Map /edu/ to 192.168.25.132:8080 and /vod/ to 192.168.25.132:8081 using regex‑based location blocks.

3.3 Load Balancing Implementation

Modify nginx.conf to define an upstream group and use the proxy_pass directive. Restart Nginx, create test files on different backend servers, and verify that requests are distributed according to the chosen method.

Load‑balancing methods:

Round‑robin (default)

Weight: higher weight gets more traffic

Fair: prefers servers with shorter response times

ip_hash: consistent client IP maps to the same backend, useful for session persistence

3.4 Static‑Dynamic Separation in Practice

Serve static files directly from Nginx while forwarding dynamic requests to Tomcat. Configure separate server blocks or location rules to achieve this separation.

4. High‑Availability with Keepalived

Install keepalived on two Nginx nodes, configure a virtual IP, and define health‑check scripts. Start the service; the virtual IP remains reachable even if the primary node fails, providing seamless failover.

5. Nginx Process Model

Nginx runs a master process that manages multiple worker processes. The number of workers should match the CPU core count. Workers operate independently, allowing hot reloads and ensuring that a failure in one worker does not affect others.

Summary

Match worker count to CPU cores for optimal performance.

One master with multiple workers enables hot deployment and fault isolation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmenthigh availabilityload balancingreverse proxyServer Configuration
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.