Nginx Overview, Installation, Core Configuration and Practical Use Cases
This article introduces Nginx as a high‑performance web and reverse‑proxy server, explains its key features, walks through installation and startup steps, details the main nginx.conf sections, and demonstrates practical configurations for reverse proxy, load balancing, and static‑dynamic separation.
1. Nginx Overview
Nginx ("engine x") is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency; major Chinese sites such as Baidu, JD, Sina, NetEase, Tencent and Taobao use it.
1.1 Web Server Nginx can serve static pages and supports CGI languages like Perl and PHP, but not Java (which requires Tomcat). It is optimized for performance and can handle up to 50,000 concurrent connections.
1.2 Reverse Proxy
Forward proxy: client configures the proxy.
Reverse proxy: server handles the proxy transparently to the client.
1.3 Load Balancing Nginx’s asynchronous framework holds many concurrent requests and forwards them to backend servers, improving security, saving public IPs, and enabling easy scaling.
1.4 Static‑Dynamic Separation Separating static and dynamic content across different servers speeds up parsing and reduces load on a single server.
2. Installation and Startup
2.1 Required Packages pcre‑8.37.tar.gz, openssl‑1.0.1t.tar.gz, zlib‑1.2.8.tar.gz, nginx‑1.11.1.tar.gz
2.2 Installation Process
1. Install PCRE: extract, ./configure, make, make install.
2. Install OpenSSL: extract, ./config, make && make install.
3. Install zlib: extract, ./configure, make && make install.
4. Install Nginx: extract, ./configure, make && make install.
Configure firewall to open HTTP port (80) and reload the firewall.
2.3 Nginx Startup Commands
Start: ./nginx in /usr/local/nginx/sbin
Stop: ./nginx -s stop
Reload: ./nginx -s reload
To enable auto‑start, add /usr/local/nginx/sbin/nginx to the Linux init script /etc/rc.d/rc .
3. Core Nginx Configuration File (nginx.conf)
The main configuration resides in the conf directory under the installation path. It is divided into three parts:
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;
}
}
}Global block sets overall server parameters such as user, worker processes, PID, and log locations.
Events block controls connection handling; the example sets worker_connections 1024 .
HTTP block contains most functional directives, including server and location blocks for virtual hosts, proxying, caching, etc.
4. Practical Configuration – Reverse Proxy
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 URLs; syntax: location [=|~|~*|^~] url { ... } with meanings for exact match, case‑sensitive/insensitive regex, and priority handling.
5. Practical Configuration – Load Balancing
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;
}
}
}Nginx supports several load‑balancing methods: round‑robin (default), weight‑based, ip_hash (session‑sticky), and third‑party fair (based on response time).
6. Practical Configuration – Static‑Dynamic Separation
Two approaches: host static files on a separate domain/server, or serve both static and dynamic content from the same server and use location rules to separate them. Use the expires directive to set client‑side caching (e.g., expires 3d ).
7. Nginx Principles and Optimization Parameters
Master‑Worker Model – each worker runs in its own process without locks, improving performance and fault isolation. If a worker crashes, other workers continue serving.
Number of Workers – set worker_processes equal to the number of CPU cores for optimal utilization.
# Set number of workers
worker_processes 4
# Bind workers to CPUs
worker_cpu_affinity 0001 0010 0100 1000Worker Connections – defines the maximum connections per worker. Total maximum connections ≈ worker_processes * worker_connections . For reverse‑proxy scenarios, each client request may use two connections (client‑side and backend‑side).
8. High‑Availability Nginx Cluster
Examples of Keepalived + Nginx clusters in master‑slave and dual‑master modes are shown (diagrams omitted).
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.