Operations 11 min read

Master Nginx: Reverse Proxy, Load Balancing, and High‑Availability Setup

This guide explains Nginx fundamentals, including its reverse‑proxy and load‑balancing capabilities, static‑dynamic separation, common command‑line usage, configuration file structure, and step‑by‑step high‑availability deployment with Keepalived, providing practical examples and diagrams for each feature.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Nginx: Reverse Proxy, Load Balancing, and High‑Availability Setup

Nginx Overview

Nginx is a high‑performance HTTP server and reverse proxy that uses little memory and can handle up to 50,000 concurrent connections.

Core Concepts

Reverse Proxy vs. Forward Proxy

Forward proxy requires client‑side configuration to reach external networks. Reverse proxy works transparently to the client, forwarding requests to backend servers and hiding their real IP addresses.

Load Balancing

Nginx can distribute incoming requests across multiple backend servers to improve scalability and reliability. Supported algorithms include simple round‑robin, weighted round‑robin, fair, and IP‑hash.

Static‑Dynamic Separation

Separating static assets from dynamic content reduces server load and speeds up page rendering: Nginx serves static files while application servers handle dynamic requests.

Installation

Reference URL: https://blog.csdn.net/yujing1314/article/details/97267369

Common Nginx Commands

./nginx -v

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

Configuration File Structure

The main nginx.conf is divided into three top‑level blocks:

Global block : settings that affect the whole server, such as worker_processes and connection limits.

Events block : network‑level options (e.g., worker_connections, multi‑worker connection serialization).

HTTP block : directives for virtual hosts, reverse proxy, load balancing, etc.

Location Directive

Matches request URIs. Syntax: location [ = | ~ | ~* | ^~ ] /url { ... } = – exact match, stop further search.

~ – case‑sensitive regular expression.

~* – case‑insensitive regular expression.

^~ – prefix match with highest priority, no regex processing.

Reverse Proxy Practical Example

1. Map the domain www.123.com to a local Tomcat listening on port 8080.

2. Use a regular‑expression location to forward http://192.168.25.132:9001/edu/ to 192.168.25.132:8080 and /vod/ to 192.168.25.132:8081.

3. After editing nginx.conf, reload Nginx ( ./nginx -s reload) and verify the routing.

server {
    listen 80;
    server_name www.123.com;
    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

server {
    listen 9001;
    location ~ ^/edu/ {
        proxy_pass http://192.168.25.132:8080;
    }
    location ~ ^/vod/ {
        proxy_pass http://192.168.25.132:8081;
    }
}

Load Balancing Practical Example

Define an upstream group and enable round‑robin distribution.

upstream tomcat_cluster {
    server 192.168.25.132:8080;
    server 192.168.25.132:8081;
    # weight, fair, ip_hash etc. can be added as needed
}

server {
    listen 80;
    location / {
        proxy_pass http://tomcat_cluster;
    }
}

Reload Nginx and place test files in each Tomcat’s webapps/edu directory to observe balanced responses.

Static‑Dynamic Separation Practical Example

Configure Nginx to serve static assets (images, CSS, JS) directly while proxying dynamic requests to Tomcat.

server {
    listen 80;
    server_name www.example.com;

    # Static files
    location /static/ {
        root /var/www/html;
        expires 30d;
    }

    # Dynamic requests
    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

High Availability with Keepalived

Deploy two Nginx instances and use Keepalived to manage a virtual IP (VIP). When the master fails, the backup takes over.

# Install Keepalived (CentOS/RHEL)
 yum install keepalived -y
 systemctl start keepalived.service

Edit /etc/keepalived/keepalived.conf (example):

global_defs {
    notification_email {
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.25.147
    router_id LVS_DEVEL
}

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

vrrp_instance VI_1 {
    state MASTER            # on the primary node, use BACKUP on the secondary
    interface eth0
    virtual_router_id 51
    priority 100            # higher priority wins
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.25.50/24
    }
    track_script {
        chk_nginx
    }
}

Start Keepalived on both nodes. The VIP 192.168.25.50 will always point to a healthy Nginx instance.

Performance Tuning

Set the number of worker processes to match the number of CPU cores (e.g., worker_processes auto;). The master process handles configuration reloads, allowing zero‑downtime updates while workers continue serving requests.

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.

high availabilityload balancingServer Configuration
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.