前言
Nginx作为现代互联网架构中最重要的Web服务器和反向代理服务器,其性能调优对企业级应用的稳定性和效率至关重要。本指南将从运维实践角度出发,详细介绍Nginx在企业环境中的各种调优策略和最佳实践。
一、基础配置调优
1.1 工作进程配置
# 根据CPU核心数设置工作进程数 worker_processesauto; # 工作进程绑定CPU核心 worker_cpu_affinityauto; # 单个工作进程最大连接数 worker_connections65535; # 工作进程最大打开文件数 worker_rlimit_nofile65535;
1.2 事件模型优化
events{
# 使用epoll事件模型(Linux系统)
useepoll;
# 允许同时接受多个新连接
multi_accepton;
# 工作进程最大连接数
worker_connections65535;
# 接受连接锁
accept_mutexoff;
}
1.3 网络连接优化
# 启用高效文件传输 sendfileon; # 优化sendfile性能 tcp_nopushon; tcp_nodelayon; # 连接保持时间 keepalive_timeout65; keepalive_requests100; # 客户端请求头超时 client_header_timeout15; # 客户端请求体超时 client_body_timeout15; # 向客户端发送响应超时 send_timeout15;
二、内存和缓冲区调优
2.1 缓冲区设置
# 客户端请求头缓冲区 client_header_buffer_size4k; large_client_header_buffers88k; # 客户端请求体缓冲区 client_body_buffer_size128k; client_max_body_size100m; # 代理缓冲区 proxy_buffer_size4k; proxy_buffers84k; proxy_busy_buffers_size8k; # FastCGI缓冲区 fastcgi_buffer_size4k; fastcgi_buffers84k; fastcgi_busy_buffers_size8k;
2.2 文件缓存配置
# 打开文件缓存 open_file_cachemax=100000inactive=20s; open_file_cache_valid30s; open_file_cache_min_uses2; open_file_cache_errorson; # 日志缓存 access_log/var/log/nginx/access.log main buffer=32kflush=5s; error_log/var/log/nginx/error.logwarn;
三、压缩优化
3.1 Gzip压缩
# 启用Gzip压缩 gzipon; gzip_varyon; gzip_min_length1000; gzip_comp_level6; gzip_proxiedany; # 压缩文件类型 gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss application/atom+xml image/svg+xml; # 压缩缓冲区 gzip_buffers168k; gzip_http_version1.1;
3.2 Brotli压缩(需要模块支持)
# 启用Brotli压缩 brotlion; brotli_comp_level6; brotli_min_length1000; brotli_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml application/atom+xml image/svg+xml;
四、SSL/TLS优化
4.1 SSL配置优化
# SSL协议版本 ssl_protocolsTLSv1.2TLSv1.3; # 加密套件 ssl_ciphersECDHE-RSA-AES128-GCM-SHA256ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384; ssl_prefer_server_cipherson; # SSL会话缓存 ssl_session_cacheshared50m; ssl_session_timeout1d; ssl_session_ticketsoff; # OCSP Stapling ssl_staplingon; ssl_stapling_verifyon; ssl_trusted_certificate/path/to/ca-bundle.crt;
4.2 HTTP/2配置
server{
listen443ssl http2;
server_nameexample.com;
# HTTP/2推送
http2_push_preloadon;
# 其他SSL配置...
}
五、负载均衡和代理优化
5.1 上游服务器配置
upstreambackend {
# 负载均衡算法
ip_hash;
# 后端服务器
server192.168.1.10:8080weight=3max_fails=3fail_timeout=30s;
server192.168.1.11:8080weight=2max_fails=3fail_timeout=30s;
server192.168.1.12:8080weight=1max_fails=3fail_timeout=30sbackup;
# 连接保持
keepalive32;
keepalive_requests100;
keepalive_timeout60s;
}
5.2 代理配置优化
location/ {
proxy_passhttp://backend;
# 代理超时设置
proxy_connect_timeout5s;
proxy_send_timeout60s;
proxy_read_timeout60s;
# 代理缓冲
proxy_bufferingon;
proxy_buffer_size4k;
proxy_buffers84k;
# 代理头信息
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
# HTTP版本
proxy_http_version1.1;
proxy_set_headerConnection"";
}
六、缓存策略
6.1 静态资源缓存
# 图片、CSS、JS缓存
location~* .(jpg|jpeg|png|gif|ico|css|js)${
expires1y;
add_headerCache-Control"public, immutable";
add_headerVary Accept-Encoding;
}
# 字体文件缓存
location~* .(woff|woff2|ttf|eot)${
expires1y;
add_headerCache-Control"public";
add_headerAccess-Control-Allow-Origin *;
}
6.2 代理缓存
# 缓存配置
proxy_cache_path/var/cache/nginx levels=1:2keys_zone=my_cache:10mmax_size=10ginactive=60muse_temp_path=off;
server{
location/ {
proxy_cachemy_cache;
proxy_cache_valid20030210m;
proxy_cache_valid4041m;
proxy_cache_use_staleerrortimeout updating http_500 http_502 http_503 http_504;
proxy_cache_lockon;
proxy_cache_lock_timeout5s;
# 缓存key
proxy_cache_key$scheme$proxy_host$request_uri;
# 缓存头信息
add_headerX-Cache-Status$upstream_cache_status;
proxy_passhttp://backend;
}
}
七、安全加固
7.1 基础安全配置
# 隐藏版本信息 server_tokensoff; # 安全头 add_headerX-Frame-Options"SAMEORIGIN"always; add_headerX-Content-Type-Options"nosniff"always; add_headerX-XSS-Protection"1; mode=block"always; add_headerReferrer-Policy"strict-origin-when-cross-origin"always; # 限制请求方法 if($request_method!~ ^(GET|HEAD|POST)$){ return405; }
7.2 请求限制
# 限制请求频率
limit_req_zone$binary_remote_addrzone=api:10mrate=10r/s;
limit_req_zone$binary_remote_addrzone=login:10mrate=1r/s;
server{
location/api/ {
limit_reqzone=api burst=20nodelay;
limit_req_status429;
}
location/login {
limit_reqzone=login burst=5nodelay;
limit_req_status429;
}
}
# 限制连接数
limit_conn_zone$binary_remote_addrzone=conn_limit_per_ip:10m;
limit_connconn_limit_per_ip10;
八、监控和日志
8.1 访问日志优化
# 自定义日志格式
log_formatmain'$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';
# 条件日志记录
map$status$loggable{
~^[23] 0;
default1;
}
access_log/var/log/nginx/access.log main buffer=32kflush=5sif=$loggable;
8.2 状态监控
# 启用状态页面
location/nginx_status {
stub_statuson;
access_logoff;
allow127.0.0.1;
allow192.168.1.0/24;
denyall;
}
九、系统级优化
9.1 内核参数调优
# /etc/sysctl.conf net.core.somaxconn = 65535 net.core.netdev_max_backlog = 5000 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 1024 65535 fs.file-max = 6815744
9.2 文件描述符限制
# /etc/security/limits.conf nginx soft nofile 65535 nginx hard nofile 65535 nginx softnproc65535 nginx hardnproc65535
十、性能监控和调优工具
10.1 监控指标
关键监控指标包括:
• 请求处理时间
• 并发连接数
• 错误率
• 内存使用情况
• CPU使用率
• 网络带宽利用率
10.2 性能测试工具
# 使用wrk进行压力测试 wrk -t12 -c400 -d30s --latency http://example.com/ # 使用ab进行基准测试 ab -n 10000 -c 100 http://example.com/ # 使用siege进行并发测试 siege -c 100 -t 30s http://example.com/
十一、最佳实践总结
1.合理配置工作进程数:通常设置为CPU核心数或使用auto自动检测
2.优化缓冲区大小:根据实际业务需求调整缓冲区大小
3.启用压缩:对文本类型资源启用gzip压缩
4.配置合理的超时时间:避免长时间占用连接
5.使用HTTP/2:提升多路复用性能
6.实施缓存策略:合理设置静态资源和代理缓存
7.定期监控和优化:持续监控性能指标并进行调优
十二、故障排查
12.1 常见问题诊断
# 检查Nginx配置 nginx -t # 查看错误日志 tail-f /var/log/nginx/error.log # 检查进程状态 ps aux | grep nginx # 查看连接状态 netstat -an | grep :80 |wc-l # 检查文件描述符使用情况 lsof -u nginx |wc-l
12.2 性能问题排查
当遇到性能问题时,应该:
1. 检查系统资源使用情况
2. 分析访问日志模式
3. 监控上游服务器响应时间
4. 检查缓存命中率
5. 分析网络连接状态
通过系统性的调优和持续的监控,可以显著提升Nginx在企业环境中的性能表现,确保业务的稳定运行。
-
互联网
+关注
关注
55文章
11321浏览量
108867 -
服务器
+关注
关注
13文章
10095浏览量
90899 -
nginx
+关注
关注
0文章
180浏览量
12971
原文标题:大厂Nginx调优秘籍:QPS提升500%的核心配置技巧
文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录

Nginx在企业环境中的调优策略
评论