Master Nginx: Architecture, Installation, Commands, and High‑Availability Guide
This comprehensive guide covers Nginx's architecture, reverse proxy and load‑balancing concepts, static‑dynamic separation, Linux installation steps, common command‑line usage, configuration file structure, practical reverse‑proxy and load‑balancing setups, high‑availability with keepalived, and the underlying master‑worker process model.
1. Nginx Knowledge Structure Diagram
Nginx is a high‑performance HTTP and reverse‑proxy server, characterized by low memory usage and strong concurrency; it can support up to 50,000 concurrent connections.
Designed for performance optimization, nginx emphasizes efficiency above all.
1.1 Reverse Proxy
Forward Proxy – In a LAN, a client cannot directly access the Internet and must use a proxy server; this is called a forward proxy.
Reverse Proxy – The client is unaware of the proxy; it sends requests to the reverse‑proxy server, which selects a target server, fetches data, and returns it to the client, exposing only the proxy address and hiding the real server IP.
1.2 Load Balancing
Clients send multiple requests to a server; the server processes them, possibly interacting with a database, and returns results.
Ordinary request/response process
As traffic and data volume grow, a single‑server architecture cannot meet demand.
Simply upgrading hardware is insufficient; adding more servers to form a cluster and distributing requests among them implements load balancing.
Load Balancing Diagram
1.3 Static‑Dynamic Separation
To speed up site parsing, dynamic and static pages can be handled by different servers, reducing pressure on a single server.
Before separation
After separation
2. How to Install Nginx on Linux
Reference: https://blog.csdn.net/yujing1314/article/details/97267369
3. Common Nginx Commands
Check version ./nginx -v Start ./nginx Stop (recommended ./nginx -s quit)
./nginx -s stop
./nginx -s quitReload configuration
./nginx -s reload4. Nginx Configuration File
The configuration file consists of three parts.
Global block – From the start of the file to the events block, setting directives that affect the overall operation of nginx.
Events block – Influences network connections, such as whether to serialize connections under multiple worker processes and whether to allow simultaneous connections.
Location directive
The syntax matches URLs:
location[ = | ~ | ~* | ^~ ] url { ... } location[ = | ~ | ~* | ^~ ] url{
}=: exact match without regex; stops further search on success.
~: case‑sensitive regex match.
~*: case‑insensitive regex match.
^~: highest‑priority prefix match; once matched, no further regex processing.
If a URL contains a regex, the ~ prefix is optional.
4.1 Reverse Proxy Practice
Configure reverse proxy – Goal: entering www.123.com in the browser redirects to the Tomcat main page.
Implementation steps (images omitted for brevity) – configure Tomcat, access from Windows, modify nginx configuration, reload nginx, and verify that requests to /edu/ and /vod/ are forwarded to ports 8080 and 8081 respectively. ./nginx -s reload Result: the same port proxies requests, displaying different pages based on the path.
4.2 Reverse Proxy Summary
First example: browser accesses www.123.com, host file resolves the IP, nginx listens on port 80 and proxies to Tomcat 8080.
Second example: accessing http://192.168.25.132:9001/edu/ forwards to 192.168.25.132:8080, while /vod/ forwards to 192.168.25.132:8081 via nginx listening on port 9001 and regex selection.
4.3 Load Balancing Practice
1. Modify nginx.conf (images omitted).
2. Restart nginx ./nginx -s reload 3. Create edu folder and a.html under Tomcat 8081.
4. Access the URL to see distribution across Tomcat instances.
Load balancing methods:
Round‑robin (default)
Weight – higher weight gets higher priority
Fair – based on backend response time
ip_hash – hashes client IP to consistently route a client to the same backend, solving session issues
4.4 Static‑Dynamic Separation Practice
What is static‑dynamic separation – Separate handling of static and dynamic requests; nginx serves static files while Tomcat processes dynamic content.
Two approaches: (1) host static files on a dedicated domain/server (mainstream), (2) mix static and dynamic files and let nginx separate them.
Diagram
Preparation – Prepare static files
and configure nginx
5. Nginx High Availability
If nginx fails
solution
Preparation steps:
Two nginx servers
Install keepalived
Configure a virtual IP
5.1 Install keepalived
yum install keepalived -y
rpm -q -a keepalived
keepalived-1.3.5-16.el7.x86_64Modify keepalived.conf (content shown below) and set the virtual IP to 192.168.25.50.
global_defs {
notification_email { acassen@firewall failover@firewall sysadmin@firewall }
notification_email_from Alexandre.Cassen@firewall
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 systemctl start keepalived.service Accessing the virtual IP succeeds; after stopping the primary nginx and keepalived, the backup server continues to serve traffic.
6. Principle Analysis
Diagram shows a master process and a worker process; the master is the manager, while workers handle actual requests.
Worker operation flow diagram
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
