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.
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 {
worker_connections 1024;
}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;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.71.167;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}Reload configuration with:
/usr/local/nginx/sbin/nginx -s reloadLoad Balancing Example
Goal: Distribute requests between two Tomcat instances (ports 8080 and 8081).
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myserver{
server 192.168.71.167:8080 weight=1;
server 192.168.71.167:8081 weight=1;
}
server {
listen 80;
server_name 192.168.71.167;
location / {
root html;
proxy_pass http://myserver;
proxy_connect_timeout 10;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}Common load‑balancing strategies include round‑robin (default), weight‑based, ip_hash , and third‑party fair modules.
Static‑Dynamic Separation Example
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 192.168.71.167;
location /www/ {
root /data/;
index index.html index.htm;
}
location /image/ {
root /data/;
autoindex on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}High‑Availability Cluster Example
Two machines run Keepalived, Nginx, and Tomcat. Keepalived provides a virtual IP (VIP) with master‑backup failover.
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 192.168.71.167
}
vrrp_script nginx_check {
script "/usr/local/src/nginx_check.sh"
interval 1
weight -30
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.71.100
}
track_script { nginx_check }
}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.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.