Master Nginx Basics: From Installation to Advanced Configuration
This comprehensive guide walks you through Nginx fundamentals, including its advantages, installation methods, core configuration hierarchy, key modules such as stub_status, random_index, sub_filter and gzip, log management, virtual host setup, HTTPS, rewrite rules, and smooth upgrade techniques, empowering you to deploy and optimize a high‑performance web server.
Nginx Overview
Nginx is a lightweight, high‑performance web server and reverse‑proxy that excels at handling massive concurrent connections using asynchronous, non‑blocking I/O and the epoll model.
Key Advantages
High concurrency and low memory usage
Efficient I/O multiplexing (epoll)
Built‑in reverse‑proxy and mail (IMAP/POP3) support
Installation
YUM Installation
yum install nginxSource Compilation
# Download source
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -xf nginx-1.20.2.tar.gz
cd nginx-1.20.2
# Configure with desired modules and prefix
./configure --prefix=/opt/nginx1.20.2 \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_gzip_module
make && make installConfiguration File Structure
# Global settings (main context)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
# Events block
events {
worker_connections 1024;
}
# HTTP block
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
# Server block (virtual host)
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}Important Modules
ngx_http_stub_status_module – provides real‑time connection statistics via /status location.
ngx_http_random_index_module – serves a random index file when random_index on; is set.
ngx_http_sub_module – replaces strings in response bodies using sub_filter.
ngx_http_gzip_module – compresses responses; enable with gzip on;.
ngx_http_headers_module – controls caching via expires directives.
ngx_http_limit_req_module and ngx_http_limit_conn_module – limit request rate and concurrent connections.
ngx_http_referer_module – implements hotlink protection.
Logging
Typical log format records client IP, user, timestamp, request line, status, bytes sent, referrer and user‑agent. Example:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;Virtual Hosts
Define separate server blocks for each domain or IP. Example:
server {
listen 80;
server_name www.example.com;
root /opt/example;
index index.html;
}
server {
listen 80;
server_name www.another.com;
root /opt/another;
index index.html;
}HTTPS
Enable SSL by adding listen 443 ssl; and specifying ssl_certificate and ssl_certificate_key files.
Rewrite Rules
Use rewrite directives for URL redirection and rewriting, e.g.:
rewrite ^/old/(.*)$ /new/$1 permanent;Smooth Upgrade
When Nginx is compiled from source, you can upgrade without dropping connections by sending kill -USR2 $master_pid, which spawns a new master process while the old one continues serving existing requests. Switch back with kill -QUIT $old_master_pid if needed.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
