Comprehensive Nginx Guide: Architecture, Reverse Proxy, Load Balancing, Static/Dynamic Separation, and High Availability
This article provides a detailed overview of Nginx, covering its high‑performance architecture, reverse‑proxy concepts, load‑balancing strategies, static‑dynamic separation techniques, common command‑line operations, configuration file structure, practical reverse‑proxy and load‑balancing examples, and a high‑availability solution using Keepalived.
Nginx is a high‑performance HTTP server and reverse‑proxy that consumes little memory and supports massive concurrent connections, often handling up to 50,000 simultaneous connections.
The knowledge map of Nginx includes concepts such as reverse proxy, load balancing, static‑dynamic separation, and high availability.
Reverse Proxy vs. Forward Proxy : Forward proxy requires client configuration to access external networks, while reverse proxy operates transparently, forwarding client requests to backend servers and hiding the real server IP.
Load Balancing : Distributes incoming requests across multiple servers to avoid a single‑point bottleneck; typical methods include round‑robin, weighted, fair, and IP‑hash.
Static/Dynamic Separation : Improves site performance by letting Nginx serve static assets while forwarding dynamic requests to application servers such as Tomcat.
Installation Reference : https://blog.csdn.net/yujing1314/article/details/97267369
Nginx Common Commands
./nginx -v ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reloadConfiguration File Structure
Three main blocks:
Global block : Settings that affect the whole Nginx process, including worker processes and connection limits.
Events block : Controls network connection handling, such as the maximum number of connections.
HTTP block : Contains server, location, proxy, and load‑balancing directives.
Example of a location directive:
location [ = | ~ | ~* | ^~ ] url {
# configuration
}Reverse Proxy Practical Example
Configure Nginx to listen on port 80 and forward www.123.com to a local Tomcat instance on port 8080, and also map http://192.168.25.132:9001/edu/ to 192.168.25.132:8080 and /vod/ to 8081 using regular‑expression locations.
Load Balancing Practical Example
Modify nginx.conf to define an upstream group and enable round‑robin distribution; after reloading Nginx, requests are split evenly among the backend Tomcat servers.
High Availability with Keepalived
[root@192 usr]# yum install keepalived -y [root@192 usr]# rpm -q -a keepalived keepalived-1.3.5-16.el7.x86_64Key Keepalived configuration (simplified):
global_defs {
notification_email { [email protected] [email protected] [email protected] }
notification_email_from [email protected]
smtp_server 192.168.25.147
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/src/nginx_check.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication { auth_type PASS auth_pass 1111 }
virtual_ipaddress { 192.168.25.50 }
}Start the service:
[root@192 sbin]# systemctl start keepalived.serviceAfter the master node fails, the backup node takes over the virtual IP, ensuring continuous access.
Master‑Worker Model
Nginx runs a master process that manages configuration and multiple worker processes (usually equal to CPU cores); workers handle client connections independently, providing hot‑deployment and fault isolation.
Summary
Understanding Nginx’s architecture, reverse‑proxy and load‑balancing capabilities, static‑dynamic separation, and high‑availability setup equips developers and ops engineers to build scalable, resilient web services.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.