Comprehensive Nginx Configuration Guide: Basics, Optimization, and Deployment
This article provides a step‑by‑step tutorial on understanding, simplifying, and optimizing Nginx configuration—including core directives, global/events/http blocks, reverse‑proxy setup, gzip compression, static‑dynamic separation, multi‑site hosting, and essential command‑line operations—complete with annotated code examples.
In the introduction, the author, a senior architect, explains that front‑end developers often need to understand Nginx and that many existing articles lack a clear overall picture, so this guide reorganizes the concepts from basic to advanced.
Original nginx.conf – The article first shows a full 118‑line configuration file, then removes all comments to reveal a concise 22‑line version, making it easier to read.
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}The annotated version adds explanatory comments in Chinese, describing the purpose of each directive such as worker processes, event handling, MIME types, sendfile, keepalive, server blocks, and location blocks.
Overall structure – Nginx is divided into three logical parts: the global block (settings before events ), the events block (network connection handling), and the http block (most of the functional configuration, including virtual hosts and proxy settings).
Simple deployment – For a basic website, only two modifications are needed: set server_name and root to point to the site’s directory, allowing Nginx to serve index.html directly.
Optimization techniques include:
History‑mode 404 handling: location / { try_files $uri $uri/ /index.html; }
Reverse proxy example: location /police/ { proxy_pass http://192.168.1.182:8852/police/; ... }
Enabling gzip compression with gzip on; and related directives.
Maintenance page via a commented rewrite ^(.*)$ /maintainace.html break; .
Hosting multiple sites on one IP by defining separate server blocks with different listen ports.
Static‑dynamic separation using dedicated location blocks for images, static assets, and other file types.
Basic Nginx commands – Installation ( yum install nginx ), checking processes ( netstat -anput | grep nginx ), starting ( nginx ), reloading ( nginx -s reload ), stopping ( nginx -s stop or nginx -s quit ), and testing configuration ( nginx -t ).
The article concludes with reference links to other tutorials and a brief disclaimer about the source of the content.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.