Mastering Nginx: From Basics to Advanced Load Balancing and Proxy Configurations

This article introduces Nginx as a high‑performance HTTP server and reverse proxy, explains its core functions such as forward and reverse proxying, load balancing, and static‑dynamic separation, provides step‑by‑step installation on Windows, Linux and Docker, details common configuration snippets, and compares Nginx with modern API gateways.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Nginx: From Basics to Advanced Load Balancing and Proxy Configurations

Introduction

Nginx is a high‑performance HTTP and reverse proxy server that also supports IMAP/POP3. It is renowned for its ability to handle massive concurrent connections, stability, rich module ecosystem, and low memory consumption.

1. Nginx Overview

● Nginx (engine x) is a high‑performance HTTP and reverse‑proxy web server, also providing IMAP/POP3/SMTP services. ● It uses little memory and has strong concurrency; tests show it can handle up to 50,000 concurrent connections. ● Installation is simple, configuration files are concise, bugs are few, and it can run 24/7 without restarts. ● Nginx is written entirely in C.

2. Nginx Functions

2.1 Proxy

Forward proxy: The client sends requests to a forward proxy, which then forwards them to the origin server, hiding client information. Nginx is not a default forward proxy but can be configured to act as one, useful for bypassing access restrictions or improving latency for overseas services.

在这里插入图片描述
在这里插入图片描述

Reverse proxy: Clients are unaware of the proxy; they send requests to the reverse proxy, which selects a target server, fetches data, and returns it, masking the real server IP. Nginx is the default reverse proxy server.

在这里插入图片描述
在这里插入图片描述

2.2 Load Balancing

When request volume exceeds a single server’s capacity, Nginx can distribute traffic across multiple servers using built‑in strategies (round‑robin, weighted round‑robin, IP‑hash) or custom extensions.

Round‑robin

在这里插入图片描述
在这里插入图片描述

Weighted round‑robin

在这里插入图片描述
在这里插入图片描述

IP‑hash (hashes client IP to consistently route to the same backend, solving session sharing issues)

在这里插入图片描述
在这里插入图片描述

2.3 Static and Dynamic Separation

Static files (css, js, images) are served directly without backend processing, allowing caching and faster response, while dynamic requests are passed to application servers.

在这里插入图片描述
在这里插入图片描述

3. Installation

Download address: https://nginx.org/en/download.html

Download

Extract to a non‑Chinese path

Run (double‑click nginx.exe or execute nginx.exe in cmd)

Test by opening http://localhost:80 in a browser

Modify listening port in conf/nginx.conf if 80 is occupied

server {
    listen       80;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm;
    }
    # ...
}

Reload configuration with nginx -s reload. Stop Nginx with nginx -s stop or nginx -s quit, or use taskkill /f /t /im nginx.exe.

3.2 Linux

Install build dependencies:

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

Download the source package, extract, configure, compile and install:

tar -zxvf nginx-1.26.0.tar.gz
cd nginx-1.26.0/
./configure
make
make install

Verify installation path with whereis nginx.

3.3 Docker

Pull the official image, run a container and map ports:

docker pull nginx
docker run --name mn -d -p 80:80 nginx

Inspect images with docker images. To mount custom files, copy them from the container:

docker cp mn:/usr/share/nginx/html /opt/mount/nginx/
docker cp mn:/etc/nginx/nginx.conf /opt/mount/nginx/

Remove the original container and create a new one with mounted configuration:

docker rm -f mn
docker run --name nginx -d \
    -p 80:80 -p 81:81 \
    -v /opt/mount/nginx/html:/usr/share/nginx/html \
    -v /opt/mount/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
    -v /opt/mount/nginx/conf.d:/etc/nginx/conf.d \
    nginx:1.26.0

4. Practical Example

Package an existing project, run two SpringBoot JARs on ports 8080 and 8081, then configure Nginx as a reverse proxy with upstream:

upstream zyy {
    server 127.0.0.1:8080 weight=1;
    server 127.0.0.1:8081 weight=1;
}
server {
    listen       80;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass  http://zyy;
    }
}
在这里插入图片描述
在这里插入图片描述

5. Configuration Details

Reference configuration (comments removed):

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Annotated version explains each directive, such as worker_processes matching CPU cores, worker_connections defining max connections per process, sendfile for zero‑copy file transmission, and keepalive_timeout for persistent connections.

5.2 Overall Understanding

The configuration is divided into three parts: global block, events block, and http block.

在这里插入图片描述
在这里插入图片描述

6. Difference Between Nginx and API Gateways

Both perform request forwarding, but API gateways add service‑level features such as authentication, rate limiting, and service discovery. In typical architectures, Nginx sits in front of one or more API gateways to provide load balancing, while the gateway handles business‑specific policies.

Programming language: Nginx is written in C; gateways (e.g., Spring Cloud Gateway) are usually Java‑based.

Feature focus: Nginx excels at reverse proxy, load balancing, caching, and SSL termination. Gateways provide richer service‑mesh capabilities like routing, circuit breaking, and centralized authentication.

Deployment: Nginx is often the traffic entry point in traditional web stacks; gateways are deployed within microservice ecosystems.

Extensibility: Extending Nginx often requires Lua or custom modules, whereas gateways can be extended with Java libraries.

In summary, Nginx handles traffic routing and load balancing, while API gateways focus on unified authentication, service discovery, and advanced traffic management.

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.

NGINXreverse proxyInstallation
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.