Comprehensive Guide to Nginx Configuration, Reverse Proxy, Load Balancing, and High‑Availability Clusters

This article provides a detailed tutorial on Nginx, covering its core features, configuration file structure, and practical examples for reverse proxy, load balancing, static‑dynamic separation, and high‑availability clustering with code snippets and deployment steps.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Comprehensive Guide to Nginx Configuration, Reverse Proxy, Load Balancing, and High‑Availability Clusters

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.

Key Functions

Forward proxy – requires client‑side configuration.

Reverse proxy – transparent to clients, hides backend server IPs.

Load balancing – distributes requests across multiple servers.

Static‑dynamic separation – serves static assets from dedicated servers to improve performance.

Configuration File Structure

The main configuration file ( /usr/local/nginx/conf/nginx.conf) consists of a global block, an events block, and an http block. worker_processes 1; The global block sets user, worker processes, PID path, and log settings.

events {<br/>    worker_connections  1024;<br/>}

The http block contains global HTTP settings, server blocks, and optional location blocks.

Reverse Proxy Example

Goal: Access Tomcat’s homepage via Nginx.

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

Reload configuration with: /usr/local/nginx/sbin/nginx -s reload Load Balancing Example

Goal: Distribute requests between two Tomcat instances (ports 8080 and 8081).

worker_processes  1;<br/><br/>events {<br/>    worker_connections  1024;<br/>}<br/><br/>http {<br/>    include       mime.types;<br/>    default_type  application/octet-stream;<br/>    sendfile        on;<br/>    keepalive_timeout  65;<br/><br/>    upstream myserver{<br/>        server 192.168.71.167:8080 weight=1;<br/>        server 192.168.71.167:8081 weight=1;<br/>    }<br/><br/>    server {<br/>        listen       80;<br/>        server_name  192.168.71.167;<br/>        location / {<br/>            root   html;<br/>            proxy_pass http://myserver;<br/>            proxy_connect_timeout 10;<br/>            index  index.html index.htm;<br/>        }<br/>        error_page   500 502 503 504  /50x.html;<br/>        location = /50x.html {<br/>            root   html;<br/>        }<br/>    }<br/>}

Common load‑balancing strategies include round‑robin (default), weight‑based, ip_hash, and third‑party fair modules.

Static‑Dynamic Separation Example

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

High‑Availability Cluster Example

Two machines run Keepalived, Nginx, and Tomcat. Keepalived provides a virtual IP (VIP) with master‑backup failover.

global_defs {<br/>  notification_email {<br/>    [email protected]<br/>    [email protected]<br/>    [email protected]<br/>  }<br/>  notification_email_from [email protected]<br/>  smtp_server 192.168.200.1<br/>  smtp_connect_timeout 30<br/>  router_id 192.168.71.167<br/>}<br/><br/>vrrp_script nginx_check {<br/>  script "/usr/local/src/nginx_check.sh"<br/>  interval 1<br/>  weight -30<br/>}<br/><br/>vrrp_instance VI_1 {<br/>  state MASTER<br/>  interface ens33<br/>  virtual_router_id 51<br/>  priority 100<br/>  advert_int 1<br/>  authentication {<br/>    auth_type PASS<br/>    auth_pass 1111<br/>  }<br/>  virtual_ipaddress {<br/>    192.168.71.100<br/>  }<br/>  track_script { nginx_check }<br/>}

The backup node has a similar configuration with state BACKUP and a lower priority.

These examples illustrate how to configure Nginx for common enterprise scenarios, from basic reverse proxy to load balancing, static‑dynamic separation, and high‑availability clustering.

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 availabilityNGINXreverse proxy
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

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.