Nginx 服务器配置与运维手册
目录
- Nginx 安装与部署
- 核心配置详解
- 虚拟主机配置
- 反向代理配置
- 负载均衡配置
- SSL/HTTPS配置
- 性能优化
- 日常运维
- 故障排查
1. Nginx 安装与部署
1.1 CentOS/RHEL 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| yum install -y yum-utils cat > /etc/yum.repos.d/nginx.repo << EOF [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key EOF
yum install -y nginx
systemctl start nginx systemctl enable nginx
|
1.2 Ubuntu/Debian 安装
1 2 3 4 5 6 7 8 9 10 11 12
| apt install -y curl gnupg2 ca-certificates lsb-release curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list
apt update apt install -y nginx
systemctl start nginx systemctl enable nginx
|
1.3 验证安装
1 2 3 4 5 6 7 8 9 10 11
| nginx -v
nginx -t
systemctl status nginx
ss -tulpn | grep nginx
|
2. 核心配置详解
2.1 配置文件结构
1 2 3 4 5 6 7 8 9 10 11
| /etc/nginx/nginx.conf
/etc/nginx/conf.d/
/var/log/nginx/
/usr/share/nginx/html/
|
2.2 nginx.conf 核心配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 10240; use epoll; multi_accept on; }
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; keepalive_timeout 65; types_hash_max_size 2048;
gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml application/json application/javascript application/xml;
include /etc/nginx/conf.d/*.conf; }
|
3. 虚拟主机配置
3.1 基于域名的虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 80; server_name example.com www.example.com; root /var/www/example; index index.html index.htm;
location / { try_files $uri $uri/ =404; }
error_page 404 /404.html; error_page 500 502 503 504 /50x.html; }
|
3.2 基于端口的虚拟主机
1 2 3 4 5 6 7 8 9 10 11
| server { listen 8080; server_name _; root /var/www/app1; }
server { listen 8081; server_name _; root /var/www/app2; }
|
3.3 基于 IP 的虚拟主机
1 2 3 4 5 6 7 8 9 10 11
| server { listen 192.168.1.10:80; server_name _; root /var/www/site1; }
server { listen 192.168.1.11:80; server_name _; root /var/www/site2; }
|
4. 反向代理配置
4.1 基础反向代理
1 2 3 4 5 6 7 8 9 10 11 12
| server { listen 80; server_name proxy.example.com;
location / { proxy_pass http://127.0.0.1:8080; 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; } }
|
4.2 带缓存的反向代理
1 2 3 4 5 6 7 8 9 10 11 12
| proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:100m max_size=10g inactive=60m;
server { location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; proxy_cache_key $scheme$request_method$host$request_uri; add_header X-Cache-Status $upstream_cache_status; } }
|
4.3 WebSocket 代理
1 2 3 4 5 6 7 8
| location /ws/ { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_read_timeout 86400; }
|
5. 负载均衡配置
5.1 轮询(默认)
1 2 3 4 5 6 7 8 9 10 11
| upstream backend { server 192.168.1.10:8080; server 192.168.1.11:8080; server 192.168.1.12:8080; }
server { location / { proxy_pass http://backend; } }
|
5.2 权重分配
1 2 3 4 5
| upstream backend { server 192.168.1.10:8080 weight=3; server 192.168.1.11:8080 weight=2; server 192.168.1.12:8080 weight=1; }
|
5.3 IP Hash
1 2 3 4 5 6
| upstream backend { ip_hash; server 192.168.1.10:8080; server 192.168.1.11:8080; server 192.168.1.12:8080; }
|
5.4 最少连接
1 2 3 4 5 6
| upstream backend { least_conn; server 192.168.1.10:8080; server 192.168.1.11:8080; server 192.168.1.12:8080; }
|
5.5 健康检查
1 2 3 4 5
| upstream backend { server 192.168.1.10:8080 max_fails=3 fail_timeout=30s; server 192.168.1.11:8080 max_fails=3 fail_timeout=30s; server 192.168.1.12:8080 backup; }
|
6. SSL/HTTPS 配置
6.1 自签名证书
1 2 3 4 5 6 7 8
| openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
|
6.2 HTTPS 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server { listen 443 ssl http2; server_name example.com;
ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
location / { root /var/www/example; index index.html; } }
server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; }
|
6.3 Let’s Encrypt 证书
1 2 3 4 5 6 7 8
| yum install -y certbot python3-certbot-nginx
certbot --nginx -d example.com -d www.example.com
certbot renew --dry-run
|
7. 性能优化
7.1 Worker 进程优化
1 2 3 4 5 6 7 8
| worker_processes auto; worker_rlimit_nofile 65535;
events { worker_connections 65535; multi_accept on; use epoll; }
|
7.2 缓存优化
1 2 3 4 5 6 7 8
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public, immutable"; }
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:100m max_size=10g;
|
7.3 Gzip 压缩
1 2 3 4 5 6
| gzip on; gzip_vary on; gzip_min_length 1024; gzip_proxied expired no-cache no-store private auth; gzip_comp_level 6; gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
|
7.4 连接优化
1 2 3 4
| keepalive_timeout 65; keepalive_requests 100; tcp_nopush on; tcp_nodelay on;
|
8. 日常运维
8.1 服务管理
1 2 3 4 5 6 7 8 9 10 11
| systemctl start nginx systemctl stop nginx systemctl restart nginx
systemctl reload nginx nginx -s reload
systemctl status nginx
|
8.2 配置测试
8.3 日志管理
1 2 3 4 5 6 7 8
| tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
logrotate -f /etc/logrotate.d/nginx
|
8.4 性能监控
1 2 3 4 5 6 7 8
| ss -s | grep nginx
curl http://localhost/nginx_status
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
|
9. 故障排查
9.1 常见问题
Nginx 无法启动
1 2 3 4 5 6 7 8
| ss -tulpn | grep :80
nginx -t
tail -50 /var/log/nginx/error.log
|
502 Bad Gateway
1 2 3 4 5 6
| systemctl status backend-service
curl -I http://backend-server:port
|
403 Forbidden
1 2 3 4 5 6
| ls -la /var/www/html
getenforce setenforce 0
|
404 Not Found
1 2 3 4 5
|
ls -la /var/www/html/filename
|
9.2 调试模式
1 2 3 4 5
| error_log /var/log/nginx/error.log debug;
systemctl restart nginx
|
文档版本: 1.0
最后更新: 2026-02-27
适用版本: Nginx 1.20+