Master Nginx in One Hour: A Quick Guide
This article introduces Nginx’s core concepts, walks through installing required packages, configuring the server, and explains key directives such as worker processes, events, and http blocks, then demonstrates practical setups for reverse proxy, load balancing, static‑dynamic separation, performance tuning, and high‑availability clustering.
1. Nginx Overview
Nginx ("engine x") is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency. It powers many large Chinese sites such as Baidu, JD.com, Sina, NetEase, Tencent, and Taobao.
2. Installation
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.gzInstallation steps (CentOS example):
Extract and ./configure pcre, then make && make install.
Extract and ./config && make && make install for OpenSSL.
Extract and ./configure && make && make install for zlib.
Extract and ./configure && make && make install for Nginx.
Open firewall ports for HTTP:
firewall-cmd --list-all
firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reloadStart, stop, and reload Nginx:
./nginx # start
./nginx -s stop # stop
./nginx -s reload # reloadTo enable auto‑start, add /usr/local/nginx/sbin/nginx to the Linux init script /etc/rc.d/rc.
3. Core Configuration File
The main configuration resides in conf/nginx.conf and is divided into three sections: global, events, and http.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}The global block sets process‑wide options such as the number of worker processes. The events block controls connection handling, e.g., worker_connections 1024 means each worker can handle up to 1024 simultaneous connections. The http block contains most functional directives, including virtual hosts ( server blocks) and location matching.
4. Reverse Proxy Example
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8001;
}
location ~ /demo1 {
proxy_pass http://localhost:8001;
}
location ~ /demo2 {
proxy_pass http://localhost:8002;
}
}The location directive matches request URIs. The syntax location [=|~|~*|^~] url { … } determines exact, case‑sensitive, case‑insensitive, or prioritized regex matching.
5. Load Balancing
Define an upstream pool and choose a distribution strategy:
http {
upstream myserver {
ip_hash;
server localhost:8080 weight=1;
server localhost:8081 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myserver;
proxy_connect_timeout 10;
}
}
}Strategies:
Round‑robin (default) : Requests are distributed sequentially; failed backends are automatically removed.
Weight : Higher weight receives proportionally more requests.
ip_hash : Requests from the same client IP are routed to the same backend, preserving session affinity.
fair (third‑party) : Requests go to the backend with the shortest response time.
6. Static/Dynamic Separation
Two common approaches:
Serve static files from a dedicated domain or server.
Serve static and dynamic content from the same host and use location rules to separate them.
Example using expires to cache static resources for three days:
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
expires 3d;
add_header Cache-Control "public";
}7. Optimization Parameters
The master‑worker architecture gives each worker its own process, eliminating lock contention and allowing independent failure handling. Recommended worker count equals the number of CPU cores.
# Set worker count
worker_processes 4;
# Bind workers to CPUs (4 workers, 4 CPUs)
worker_cpu_affinity 0001 0010 0100 1000;Maximum connections per worker are defined by worker_connections. The theoretical maximum concurrent connections is worker_processes × worker_connections. For reverse‑proxy scenarios, each client request creates two connections (client‑to‑Nginx and Nginx‑to‑backend), so the effective limit is roughly half of that product.
8. High‑Availability Cluster
Two Keepalived‑based HA patterns are shown:
Master‑slave mode:
Dual‑master mode:
These setups provide automatic failover and load distribution for Nginx services.
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.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
