Operations 12 min read

Mastering Nginx: Reverse Proxy, Load Balancing, and High Availability Explained

This guide explains Nginx’s high‑performance architecture, covering reverse and forward proxy concepts, load‑balancing strategies, static‑dynamic separation, installation steps, common commands, configuration blocks, and a practical high‑availability setup using Keepalived, providing clear examples and diagrams for each feature.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Nginx: Reverse Proxy, Load Balancing, and High Availability Explained

Introduction to Nginx

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.

Nginx Knowledge Map

Reverse Proxy vs. Forward Proxy

Forward proxy : Clients inside a LAN cannot access the Internet directly and must go through a proxy server.

Reverse proxy : Clients send requests to the reverse‑proxy server without any configuration; the proxy forwards the request to the appropriate backend server and returns the response, hiding the real server IP.

Load Balancing

When request volume grows, a single server cannot keep up. Adding more servers and distributing requests among them implements load balancing.

Example: 15 requests arriving at the proxy are evenly split among three backend servers, each handling five requests.

Static‑Dynamic Separation (动静分离)

To speed up site rendering, static files are served by Nginx while dynamic requests are handled by an application server such as Tomcat.

Nginx Installation on Linux

Download and extract the source package, then compile and install, or use the system package manager.

Common Nginx Commands

./nginx -v
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

Configuration File Structure

The nginx.conf file consists of three main blocks:

1. Global block

Settings that affect the entire Nginx process, such as worker processes and connection limits.

2. Events block

Controls how Nginx handles network connections, e.g., enabling connection serialization across workers.

3. HTTP block

Contains server definitions, reverse‑proxy rules, and load‑balancing directives.

location [ = | ~ | ~* | ^~ ] url {
    
}

Explanation of the location modifiers:

= : exact match, stops further search.

~ : case‑sensitive regex match.

~* : case‑insensitive regex match.

^~ : highest‑priority prefix match, stops further search.

Reverse Proxy Practical Example

Goal: Access www.123.com in a browser and have Nginx forward the request to a Tomcat instance on port 8080.

After configuring the server and location / blocks, requests to www.123.com are proxied to http://127.0.0.1:8080.

Load Balancing Practical Example

Modify nginx.conf to define an upstream group and use the proxy_pass directive. After reloading Nginx, requests are distributed among multiple Tomcat instances.

Load‑balancing methods supported:

Round‑robin (default)

Weight (higher weight = higher priority)

Fair (based on response time)

IP‑hash (consistent client‑to‑server mapping)

Static‑Dynamic Separation Practical Setup

Configure Nginx to serve static assets directly while forwarding dynamic requests to Tomcat.

High Availability with Keepalived

Deploy two Nginx nodes and install Keepalived to provide a virtual IP that floats between the nodes.

yum install keepalived -y
rpm -q -a keepalived
keepalived-1.3.5-16.el7.x86_64
cd /etc/keepalived
vi keepalived.conf
global_defs {
    notification_email { [email protected] }
    notification_email_from [email protected]
    smtp_server 192.168.25.147
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/src/nginx_check.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { 192.168.25.50 }
}
systemctl start keepalived.service

After starting the service, the virtual IP remains reachable even if the primary Nginx node fails.

Worker and Master Architecture

Nginx runs a master process that manages one or more worker processes. The number of workers should match the CPU core count; workers operate independently, so a failure in one does not affect the others.

Conclusion

This comprehensive guide demonstrates how to install Nginx, configure reverse proxy, load balancing, static‑dynamic separation, and achieve high availability with Keepalived, providing the essential commands, configuration snippets, and visual diagrams needed for reliable production deployments.

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.

keepalived
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.