Mastering Nginx: From Basics to High‑Availability Clusters
This article introduces Nginx’s high‑performance architecture, explains its role as a web server, reverse proxy, and load balancer, walks through installation, core configuration, worker tuning, practical reverse‑proxy and load‑balancing examples, and demonstrates building a high‑availability cluster with Keepalived.
Nginx Overview
Nginx (engine x) is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency, used by major Chinese sites such as Baidu, JD, Sina, NetEase, Tencent, and Taobao.
1.1 Web Server
Nginx can serve static pages and, via CGI, dynamic languages like Perl and PHP, but not Java (requires Tomcat). It can handle up to 50,000 concurrent connections.
1.2 Reverse Proxy
Forward proxy: client configures proxy.
Reverse proxy: server handles proxy transparently.
1.3 Load Balancing
Nginx’s asynchronous architecture holds many concurrent requests and forwards them to backend servers, enabling safer host hiding, IP saving, and easy scaling.
1.4 Static‑Dynamic Separation
Separating static and dynamic content onto different servers reduces load and speeds up parsing.
Nginx serves static resources; Tomcat serves dynamic resources.
Installation and Startup
Required source packages: pcre‑8.37.tar.gz, openssl‑1.0.1t.tar.gz, zlib‑1.2.8.tar.gz, nginx‑1.11.1.tar.gz.
Installation steps include compiling pcre, openssl, zlib, then nginx with ./configure, make, make install.
Open firewall ports (e.g., firewall-cmd --add-service=http) and reload.
Nginx Startup Commands
Start: ./nginx in /usr/local/nginx/sbin
Stop: ./nginx -s stop Reload:
./nginx -s reloadSet Nginx as a Service
Edit /etc/rc.d/rc and add /usr/local/nginx/sbin/nginx.
Core Configuration File (nginx.conf)
The default configuration resides in the conf directory. It consists of three blocks: global, events, and http.
worker_processes 1;</code><code>events {</code><code> worker_connections 1024;</code><code>}</code><code>http {</code><code> include mime.types;</code><code> default_type application/octet-stream;</code><code> keepalive_timeout 65;</code><code> server {</code><code> listen 80;</code><code> server_name localhost;</code><code> location / {</code><code> root html;</code><code> index index.html index.htm;</code><code> }</code><code> }</code><code>}Explanation of each block follows.
Global Block
Sets directives affecting the whole server, such as user, worker_processes, PID path, and log locations.
Events Block
Controls connection handling; example sets worker_connections 1024.
HTTP Block
Contains most functional directives, including include, MIME types, logging, timeouts, and server definitions.
Server and Location Blocks
Define virtual hosts and request routing. Multiple location directives can match URLs with regex modifiers (=, ~, ~*, ^~).
Practical Configurations
Reverse Proxy Example
server {</code><code> listen 80;</code><code> server_name localhost;</code><code> location / {</code><code> proxy_pass http://localhost:8001;</code><code> }</code><code> location ~ /demo1 {</code><code> proxy_pass http://localhost:8001;</code><code> }</code><code> location ~ /demo2 {</code><code> proxy_pass http://localhost:8002;</code><code> }</code><code>}Load Balancing Example
http{</code><code> upstream myserver{</code><code> ip_hash;</code><code> server localhost:8080 weight=1;</code><code> server localhost:8081 weight=1;</code><code> }</code><code> server{</code><code> listen 80;</code><code> server_name localhost;</code><code> location / {</code><code> proxy_pass http://myserver;</code><code> proxy_connect_timeout 10;</code><code> }</code><code> }</code><code>}Load‑balancing methods include round‑robin (default), weight, ip_hash, and third‑party fair.
Worker Process Tuning
Set worker_processes equal to CPU cores and configure worker_cpu_affinity for binding.
# Set worker number</code><code>worker_processes 4</code><code># Bind workers to CPUs</code><code>worker_cpu_affinity 0001 0010 0100 1000 worker_connectionsdefines the maximum connections per worker; total possible connections equal worker_processes * worker_connections.
High‑Availability Cluster
Combine Keepalived with Nginx for active‑passive (master‑slave) or active‑active (dual‑master) clusters.
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
