Nginx 配置檔結構清晰、模組化程度高。本文以完整範例逐段解析核心指令含義、調優建議與最佳實踐,涵蓋主模組、事件模組、HTTP 核心、上游負載均衡、虛擬主機、反向代理參數、狀態監控與安全防護。
user www-data; # 執行 worker 進程的使用者/群組
worker_processes auto; # 自動偵測 CPU 核心數 (建議)
error_log /var/log/nginx/error.log warn; # 錯誤日誌等級
pid /var/run/nginx.pid; # 主進程 PID 檔
events {
use epoll; # Linux 2.6+ 高效 I/O 多工模型
worker_connections 1024; # 單 worker 最大連接數
# multi_accept on; # 一次 accept 多個連接 (高併發建議開啟)
}
最大並發連接數 ≈ worker_processes × worker_connections (雙向連接再除 2)。
http {
include /etc/nginx/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; # 零拷貝傳輸靜態檔
tcp_nopush on; # 封包堆疊發送
tcp_nodelay on; # 關閉 Nagle 演算法,低延遲
keepalive_timeout 65; # 長連接超時
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
# Gzip 壓縮
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable "MSIE [1-6]\\.";
# 包含站點配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
upstream backend {
# 權重輪詢 (預設)
server 192.168.1.10:80 weight=3;
server 192.168.1.11:80 weight=2;
server 192.168.1.12:80 weight=1 backup; # 備援節點
# 其他策略
# least_conn; # 最少連接數
# ip_hash; # 依客戶端 IP 雜湊 (Session 親和性)
# hash $request_uri; # 依請求 URI 雜湊
keepalive 32; # 到上游的長連接數
}
server {
listen 80;
server_name www.example.com example.com;
# 靜態資源直接服務
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|txt)$ {
root /var/www/example.com;
expires 30d;
add_header Cache-Control "public";
}
# 動態請求反向代理
location / {
proxy_pass https://backend;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 緩衝區調優
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 32k;
proxy_busy_buffers_size 64k;
# 超時控制
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 健康檢查與故障轉移 (需 nginx plus 或 upstream 模組支援)
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
# 狀態監控 (需編譯 --with-http_stub_status_module)
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
}
# 安全:隱藏版本號、禁止隱藏檔
server_tokens off;
location ~ /\. { deny all; }
}
| 指令 | 建議值/說明 |
|---|---|
| worker_processes | auto 或 CPU 核心數 |
| worker_connections | 1024~4096 (視 ulimit -n) |
| sendfile | on (靜態檔) / off (大檔下載平衡磁碟網路) |
| keepalive_timeout | 60~75 秒 |
| gzip_types | 包含 text/css/js/json/xml 等 |
| proxy_buffers | 8 32k 或 4 64k (視平均回應大小) |
| proxy_busy_buffers_size | proxy_buffers × 2 |
| client_max_body_size | 10m~100m (視上傳需求) |
nginx -t # 語法檢查
nginx -s reload # 平滑重載 (不中斷連接)
nginx -s quit # 優雅關閉
依據流量特性調整參數,善用 stub_status + Prometheus 監控關鍵指標 (active connections, requests/sec, upstream latency),持續優化 Nginx 效能。