1. Nginx配置实战经验分享
作为一款高性能的Web服务器和反向代理服务器,Nginx在现代Web架构中扮演着重要角色。我在过去五年的运维工作中,累计处理过300+台Nginx服务器的配置工作,今天就把那些真正高频使用、经过生产环境验证的配置方案整理出来。这些配置不是教科书式的示例,而是经过实战检验、能直接解决实际问题的方案。
2. 基础配置优化
2.1 性能调优核心参数
worker_processes和worker_connections是Nginx性能的基石。我通常这样设置:
nginx复制worker_processes auto; # 自动匹配CPU核心数
worker_connections 1024; # 每个worker最大连接数
events {
use epoll; # Linux下高性能事件模型
multi_accept on; # 同时接受多个新连接
}
注意:worker_connections值需要根据服务器内存调整,每个连接大约消耗10KB内存
2.2 缓冲区与超时优化
这些参数能显著改善高并发下的性能表现:
nginx复制client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m; # 文件上传大小限制
large_client_header_buffers 4 4k;
keepalive_timeout 30; # 长连接保持时间
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
3. 安全防护配置
3.1 基础安全加固
nginx复制server_tokens off; # 隐藏Nginx版本号
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
3.2 防DDoS配置
nginx复制limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
limit_req zone=one burst=5 nodelay;
limit_conn addr 10;
}
4. 常用场景配置
4.1 静态资源服务
nginx复制location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
access_log off;
add_header Cache-Control "public";
try_files $uri =404;
}
4.2 反向代理配置
nginx复制location /api/ {
proxy_pass http://backend_server;
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_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering off;
}
5. HTTPS最佳实践
5.1 SSL证书配置
nginx复制ssl_certificate /path/to/fullchain.pem;
ssl_certificate_key /path/to/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
ssl_prefer_server_ciphers on;
5.2 HTTP自动跳转HTTPS
nginx复制server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
6. 日志与监控配置
6.1 自定义日志格式
nginx复制log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log main buffer=32k flush=1m;
error_log /var/log/nginx/error.log warn;
6.2 状态监控页面
nginx复制location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
}
7. 高级配置技巧
7.1 负载均衡配置
nginx复制upstream backend {
least_conn; # 最少连接算法
server 10.0.0.1:8080 weight=5;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup; # 备用服务器
}
server {
location / {
proxy_pass http://backend;
health_check interval=10 fails=3 passes=2;
}
}
7.2 多域名配置
nginx复制server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;
# 其他配置...
}
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /path/to/api.example.com.crt;
ssl_certificate_key /path/to/api.example.com.key;
# 其他配置...
}
8. 常见问题排查
8.1 502 Bad Gateway问题
可能原因及解决方案:
- 后端服务不可用:检查后端服务状态和日志
- 代理超时设置过短:调整proxy_read_timeout
- 缓冲区不足:增加proxy_buffer_size
8.2 性能瓶颈分析
使用工具定位:
bash复制# 实时监控连接状态
nginx -T | grep worker_connections
ss -s | grep -i nginx
# 分析慢请求
awk '{if($NF>1)print}' /var/log/nginx/access.log | sort -nk10
9. 配置管理建议
- 使用include指令模块化配置:
nginx复制include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
- 配置版本控制:
bash复制cd /etc/nginx
git init
git add .
git commit -m "Initial nginx config"
- 配置检查与重载:
bash复制nginx -t # 测试配置
nginx -s reload # 平滑重载
在实际运维中,我发现很多问题都源于对基础配置的理解不足。比如worker_connections设置过高导致内存溢出,或者keepalive_timeout设置不合理引发连接堆积。建议每次修改配置后,至少观察以下指标:活跃连接数(nginx -s status)、内存占用(top -p pgrep nginx)、错误日志(/var/log/nginx/error.log)。