乐闻世界logo
搜索文章和话题

Nginx 如何进行性能调优?有哪些关键参数?

2月21日 16:57

Nginx 如何进行性能调优?有哪些关键参数?

Nginx 性能调优是一个系统工程,需要从多个维度进行优化。合理的配置可以显著提升 Nginx 的处理能力和响应速度。

核心配置优化:

nginx
# 全局配置 user nginx; worker_processes auto; # 自动设置为 CPU 核心数 worker_rlimit_nofile 100000; # 文件描述符限制 worker_cpu_affinity auto; # CPU 亲和性绑定 events { worker_connections 65535; # 每个 worker 的最大连接数 use epoll; # Linux 使用 epoll multi_accept on; # 同时接受多个连接 accept_mutex off; # 关闭互斥锁,减少锁竞争 } http { # 基础优化 sendfile on; # 启用高效文件传输 tcp_nopush on; # 优化数据包发送 tcp_nodelay on; # 禁用 Nagle 算法 keepalive_timeout 65; # 长连接超时 keepalive_requests 100; # 长连接最大请求数 # 缓冲区优化 client_body_buffer_size 128k; # 客户端请求体缓冲区 client_max_body_size 10m; # 最大请求体大小 client_header_buffer_size 1k; # 客户端请求头缓冲区 large_client_header_buffers 4 4k; # 大请求头缓冲区 # 输出缓冲 output_buffers 1 32k; # 输出缓冲区 postpone_output 1460; # 延迟输出 # 文件缓存 open_file_cache max=100000 inactive=20s; # 文件描述符缓存 open_file_cache_valid 30s; # 缓存验证间隔 open_file_cache_min_uses 2; # 最小使用次数 open_file_cache_errors on; # 缓存错误信息 # Gzip 压缩 gzip on; gzip_vary on; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss; gzip_disable "msie6"; }

Worker 进程优化:

nginx
# 根据 CPU 核心数设置 worker_processes auto; # 绑定 CPU 核心(手动设置) # 假设 4 核 CPU worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; # 设置工作进程优先级 worker_priority -5; # -20 到 19,数值越小优先级越高

连接优化:

nginx
events { # 增加连接数 worker_connections 65535; # 同时接受多个连接 multi_accept on; # 关闭互斥锁(高并发时) accept_mutex off; # 使用高效的事件模型 use epoll; # Linux # use kqueue; # BSD/macOS } http { # 长连接优化 keepalive_timeout 65; keepalive_requests 100; # 上游服务器长连接 upstream backend { server 192.168.1.100:8080; keepalive 32; # 保持 32 个空闲连接 } # 代理超时设置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }

缓冲区优化:

nginx
http { # 客户端缓冲区 client_body_buffer_size 128k; client_max_body_size 10m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; # 代理缓冲区 proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; proxy_busy_buffers_size 8k; # FastCGI 缓冲区 fastcgi_buffer_size 4k; fastcgi_buffers 8 4k; # 输出缓冲 output_buffers 1 32k; postpone_output 1460; }

文件操作优化:

nginx
http { # 文件缓存 open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; # 高效文件传输 sendfile on; tcp_nopush on; tcp_nodelay on; # 直接 I/O(大文件) # directio 4m; }

压缩优化:

nginx
http { gzip on; gzip_vary on; gzip_min_length 1024; # 最小压缩文件大小 gzip_comp_level 6; # 压缩级别 1-9 gzip_buffers 16 8k; # 压缩缓冲区 gzip_http_version 1.1; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml application/atom+xml image/svg+xml; gzip_disable "msie6"; # 静态资源预压缩 gzip_static on; }

日志优化:

nginx
http { # 自定义日志格式 log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt=$request_time uct="$upstream_connect_time" ' 'uht="$upstream_header_time" urt="$upstream_response_time"'; # 访问日志 access_log /var/log/nginx/access.log main buffer=32k flush=5s; # 错误日志 error_log /var/log/nginx/error.log warn; # 关闭静态资源日志 location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2)$ { access_log off; } }

SSL/TLS 优化:

nginx
server { listen 443 ssl http2; # SSL 会话缓存 ssl_session_cache shared:SSL:50m; ssl_session_timeout 1d; ssl_session_tickets off; # SSL 缓冲区 ssl_buffer_size 4k; # 协议和加密套件 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers on; # OCSP Stapling ssl_stapling on; ssl_stapling_verify on; }

系统级优化:

bash
# /etc/sysctl.conf # 文件描述符 fs.file-max = 1000000 # TCP 参数 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 65536 4194304 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_tw_reuse = 1 # 应用配置 sysctl -p

用户限制:

bash
# /etc/security/limits.conf nginx soft nofile 65535 nginx hard nofile 65535

监控和诊断:

nginx
# 状态监控 location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } # 请求追踪 location /debug { add_header X-Request-ID $request_id; add_header X-Upstream-Addr $upstream_addr; }

性能测试工具:

bash
# wrk 压力测试 wrk -t12 -c4000 -d30s http://example.com/ # ab 压力测试 ab -n 10000 -c 1000 http://example.com/ # siege 压力测试 siege -c 100 -t 60S http://example.com/

关键性能指标:

  1. QPS(每秒查询数):衡量处理能力
  2. 响应时间:平均、P95、P99
  3. 并发连接数:当前活动连接
  4. 错误率:4xx、5xx 错误比例
  5. CPU 使用率:不应持续超过 70%
  6. 内存使用:监控内存占用
  7. 磁盘 I/O:监控读写性能

调优建议:

  1. 渐进式调优:每次只调整一个参数,观察效果
  2. 基准测试:调优前后进行性能对比
  3. 监控指标:持续关注关键性能指标
  4. 日志分析:分析访问日志发现瓶颈
  5. 定期审查:定期检查配置是否合理
标签:Nginx