Master Nginx: Core Config, Reverse Proxy, Load Balancing & Static/Dynamic Separation

This guide introduces Nginx’s essential configuration blocks, explains how to set up reverse proxy, outlines six load‑balancing strategies, demonstrates static‑dynamic content separation, and provides common directives such as return, rewrite, error_page, logging, and access control for beginners.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
Master Nginx: Core Config, Reverse Proxy, Load Balancing & Static/Dynamic Separation

Nginx is widely used in J‑one and Jdos environments; this article explains its common configuration and basic functions for beginners.

1. Core Configuration

The main configuration file conf/nginx.conf is divided into several blocks:

Global block : global settings related to Nginx operation.

events block : network connection settings.

http block : proxy, cache, logging, virtual host, etc.

server block : parameters for a virtual host; an http block can contain multiple server blocks.

location block : request routing and page handling.

Configuration hierarchy diagram:

Example configuration file:

# Global configuration
#user administrator administrators;  # user/group, default nobody
#worker_processes 2;               # number of processes, default 1
#pid /nginx/pid/nginx.pid;         # PID file location
error_log log/error.log debug;    # log path and level

events {
    accept_mutex on;               # serialize connections
    multi_accept on;               # accept multiple connections per process
    #use epoll;                    # event model
    worker_connections 1024;       # max connections
}

http {
    include mime.types;
    default_type application/octet-stream;
    log_format myFormat '$remote_addr-$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
    access_log log/access.log myFormat;
    sendfile on;
    sendfile_max_chunk 100k;
    keepalive_timeout 65;

    upstream mysvr {
        server 127.0.0.1:7878;
        server 192.168.10.121:3333 backup; # hot standby
    }
    error_page 404 https://www.baidu.com;

    server {
        keepalive_requests 120;
        listen 4545;
        server_name 127.0.0.1;
        location ~*^.+$ {
            proxy_pass http://mysvr;
            deny 127.0.0.1;
            allow 172.18.5.54;
        }
    }
}

2. Reverse Proxy

A reverse proxy receives external requests and forwards them to internal servers, returning the server’s response to the client. It is implemented with the proxy_pass directive.

server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://localhost:8081;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
    }
}

Visiting localhost forwards the request to localhost:8081.

3. Load Balancing

When multiple backend servers exist, Nginx can distribute requests according to various strategies. Six common methods are covered:

Round Robin (RR) : default, sequentially distributes requests.

Backup (Hot standby) : secondary server used only when primary fails.

Weight : servers receive requests proportionally to their weight.

ip_hash : same client IP always reaches the same backend, useful for session persistence.

fair (third‑party): prefers servers with shorter response times.

url_hash (third‑party): hashes the request URI to select a backend.

Examples:

# Round Robin
upstream web_servers {
    server localhost:8081;
    server localhost:8082;
}
server {
    listen 80;
    server_name localhost;
    location / { proxy_pass http://web_servers; }
}

# Backup
upstream web_servers {
    server 127.0.0.1:7878;
    server 192.168.10.121:3333 backup;
}

# Weight
upstream web_servers {
    server localhost:8081 weight=1;
    server localhost:8082 weight=2;
}

# ip_hash
upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}

# fair (requires third‑party module)
upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}

# url_hash (requires third‑party module)
upstream backend {
    hash_method crc32;
    hash $request_uri;
    server localhost:8080;
    server localhost:8081;
}

4. Static/Dynamic Separation

Static/dynamic separation splits static files and dynamic requests onto different backends to improve performance and maintainability.

upstream web_servers {
    server localhost:8081;
    server localhost:8082;
}
server {
    listen 80;
    server_name localhost;
    set $doc_root /usr/local/var/www;
    location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
        root $doc_root/img;
    }
    location / {
        proxy_pass http://web_servers;
        proxy_set_header Host $host:$server_port;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html { root $doc_root; }
}

Accessing http://localhost/test.jpg returns the image from /usr/local/var/www/img, while http://localhost/index.html is proxied to the backend application server.

5. Other Common Directives

return : sends a status code, optional text, or redirect URL.

rewrite : modifies the request URI using regex with flags (last, break, redirect, permanent).

error_page : defines custom error pages.

log_format / access_log : configures request logging; enable compression with gzip on.

allow / deny : controls client IP access.

Built‑in variables (e.g., $remote_addr, $request_uri, $host, $server_name) can be used throughout the configuration.

6. Conclusion

Nginx is a high‑performance reverse proxy server; mastering its core configuration, reverse proxy, load balancing, and static/dynamic separation is essential. This article covered the basics, while deeper topics such as the Nginx core and internal mechanisms remain for future study.

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.

load balancingConfigurationNGINXreverse proxyWeb server
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

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.