啟動: /usr/local/nginx/sbin/nginx
停止: nginx -s stop / quit
重載: nginx -s reload
指定配置: nginx -c /path/nginx.conf
查看版本: nginx -v / -V
測試配置: nginx -t
user www www;
worker_processes 8; # CPU 核心數倍數
worker_cpu_affinity 00000001 ...; # 綁定 CPU 核心
worker_rlimit_nofile 65535; # 文件描述符限制
events {
use epoll; # Linux 2.6+ 高性能 I/O 模型
worker_connections 65535; # 單進程最大連接數
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on; # 高效文件傳輸
tcp_nopush on; tcp_nodelay on; # 防止網絡阻塞
keepalive_timeout 120; # 長連接超時
# FastCGI 優化參數...
gzip on; # 啟用壓縮
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
upstream backend {
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
}
server {
listen 80;
server_name www.example.com;
location / { proxy_pass https://backend; ... }
location ~ .*\.(gif|jpg|png)$ { expires 10d; }
location ~ .*\.(js|css)$ { expires 1h; }
# 狀態頁、動靜分離、反向代理細節...
}
}
| 參數 | 建議值 | 說明 |
|---|---|---|
| worker_processes | CPU 核心數倍數 | 減少上下文切換 |
| worker_cpu_affinity | 綁定核心 | 降低調度開銷 |
| worker_rlimit_nofile | ulimit -n 一致 | 避免 "too many open files" |
| worker_connections | 65535 | 最大連接數 = processes × connections |
| keepalive_timeout | 60s | 平衡連接復用與資源佔用 |
| open_file_cache | max=100000 inactive=20s | 緩存文件描述符 |
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
# 完整優化配置見原文...
應用後執行 sysctl -p 生效。