作为高性能的Web服务器和反向代理,Nginx的优化配置可以显著提升服务性能和稳定性。以下是一些关键的优化方法:
worker_processes auto; # 自动设置为CPU核心数
worker_cpu_affinity auto; # CPU亲和性(自动绑定)
worker_rlimit_nofile 65535; # 每个worker能打开的文件描述符数量
events {
worker_connections 10240; # 每个worker的最大连接数
use epoll; # Linux下使用epoll事件模型
multi_accept on; # 一次接受所有新连接
accept_mutex on; # 启用连接互斥锁
}
http {
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 8k;
# 文件传输优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 超时设置
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
}
server {
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
access_log off;
}
}
gzip on;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# 访问日志格式优化
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 $pipe';
# 错误日志级别
error_log /var/log/nginx/error.log warn;
# 静态资源不记录访问日志
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
access_log off;
}
# 隐藏Nginx版本号
server_tokens off;
# 防止点击劫持
add_header X-Frame-Options SAMEORIGIN;
# 防止MIME类型嗅探
add_header X-Content-Type-Options nosniff;
# 启用XSS保护
add_header X-XSS-Protection "1; mode=block";
# CSP策略(根据实际需求调整)
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.example.com; img-src 'self' data: https://*.example.com";
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
}
}
upstream backend {
least_conn; # 最少连接算法
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com max_fails=3 fail_timeout=30s;
keepalive 32; # 保持连接池
}
内核参数调整:
# 增加文件描述符限制
echo "fs.file-max = 65535" >> /etc/sysctl.conf
# TCP/IP栈优化
echo "net.ipv4.tcp_max_syn_backlog = 8192" >> /etc/sysctl.conf
echo "net.core.somaxconn = 8192" >> /etc/sysctl.conf
echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
# 应用修改
sysctl -p
文件系统优化:
noatime,nodiratime
选项Nginx状态监控:
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
性能分析工具:
nginx -t
- 测试配置strace
- 系统调用跟踪htop
/top
- 资源监控ab
/wrk
- 压力测试根据实际业务需求和服务器环境,选择适合的优化配置组合,并通过压力测试验证优化效果。