Nginx性能调优与深度监控

张开发
2026/5/13 13:36:23 15 分钟阅读

分享文章

Nginx性能调优与深度监控
静态缓存功能设置对图片、CSS、JS等静态资源设置缓存减少重复请求和磁盘IO开销提升访问速度。编辑Nginx配置server段添加静态资源缓存规则nginxserver {listen 80;server_name localhost;root /usr/local/nginx/html; # 静态资源根目录可按需调整# 匹配常见静态资源格式location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|svg|ttf|eot)$ {expires 365d; # 缓存有效期1年access_log off; # 关闭静态资源访问日志降低IOadd_header Cache-Control public, max-age31536000; # 缓存控制头open_file_cache max10000 inactive30s; # 缓存文件元数据}location / { index index.html index.htm; } # 默认首页}配置生效bashnginx -t nginx -s reload设置连接超时快速回收无效连接避免空闲连接占用系统资源优化连接复用效率。编辑Nginx配置http段nginxhttp {keepalive_timeout 65; # 客户端与Nginx长连接超时时间30-60s为宜keepalive_requests 10000; # 单长连接最大处理请求数减少建连开销client_header_timeout 10s; # 等待客户端请求头超时时间client_body_timeout 10s; # 等待客户端请求体超时时间send_timeout 10s; # 向客户端发送响应超时时间# 反向代理场景需添加以下配置# proxy_connect_timeout 10s; # 连接后端超时# proxy_read_timeout 30s; # 读取后端响应超时# proxy_send_timeout 10s; # 向后端发送请求超时}配置生效bashnginx -t nginx -s reload日志切割Nginx默认不自动切割日志日志过大会占用大量磁盘空间通过logrotate实现定时切割。创建配置文件创建logrotate配置文件指定Nginx日志路径和切割规则bashvi /etc/logrotate.d/nginxini/usr/local/nginx/logs/access.log /usr/local/nginx/logs/error.log {daily; # 每日切割rotate 7; # 保留7天日志missingok; # 日志不存在忽略错误notifempty; # 日志为空不切割compress; # 压缩切割后的日志delaycompress; # 延迟压缩当天日志postrotate# 切割后重启Nginx生成新日志[ -f /usr/local/nginx/logs/nginx.pid ] kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)endscript}测试与验证手动测试切割效果确认系统定时任务正常bashlogrotate -f /etc/logrotate.d/nginx # 手动执行切割cat /etc/cron.daily/logrotate # 确认定时任务存在配置网页压缩启用Gzip压缩减少网页传输带宽提升加载速度兼容所有浏览器。编辑Nginx配置http段nginxhttp {gzip on; # 启用Gzip压缩gzip_min_length 1024; # 仅压缩大于1KB的文件避免浪费CPUgzip_comp_level 3; # 压缩等级1-93为平衡值# 需压缩的文件类型gzip_types text/plain text/css application/javascript application/json application/xml;gzip_vary on; # 告知客户端支持压缩# 可选Brotli压缩压缩率更高需提前编译对应模块}配置生效并验证bashnginx -t nginx -s reloadbashcurl -I -H Accept-Encoding: gzip http://服务器IP # 响应头含Content-Encoding: gzip即生效Nginx的深度监控GoAccess 简介开源轻量实时日志分析工具无需依赖数据库可直接解析Nginx日志生成直观的HTML报告支持实时监控访问量、状态码、请求耗时等核心指标快速定位访问异常。GoAccess安装CentOS系统通过EPEL源安装简单高效安装后验证版本bashyum install -y epel-release # 安装EPEL源默认无GoAccess软件源yum install -y goaccessgoaccess --version # 验证安装成功配置中文环境安装中文语言包配置环境变量确保GoAccess报告显示中文bashyum install -y langpacks-zh_CN # 安装中文语言包echo LANGzh_CN.UTF-8 /etc/profile # 永久设置中文环境source /etc/profile # 立即生效echo $LANG # 验证输出zh_CN.UTF-8即成功GoAccess生成中文报告支持实时报告动态更新和静态报告固定快照按需生成。实时报告实时解析日志每秒更新数据适合实时监控场景bashgoaccess /usr/local/nginx/logs/access.log \--real-time-html --output/usr/local/nginx/html/nginx-real-time-report.html \--log-formatCOMBINED --html-report-titleNginx实时访问监控报告静态报告生成日志固定快照适合存档、分享无需实时更新bashgoaccess /usr/local/nginx/logs/access.log \--output/usr/local/nginx/html/nginx-static-report.html \--log-formatCOMBINED --html-report-titleNginx访问日志静态报告测试访问确保Nginx正常运行通过浏览器访问报告页面bashnginx # 确保Nginx启动systemctl status nginx # 查看运行状态访问地址实时报告http://服务器IP/nginx-real-time-report.html静态报告http://服务器IP/nginx-static-report.html注无法访问需开放80端口执行命令firewall-cmd --permanent --add-port80/tcp; firewall-cmd --reload。定时生成静态报告通过crontab定时任务每日凌晨生成静态报告便于日志存档bashcrontab -e # 编辑定时任务# 添加以下内容每日0点执行文件名含日期0 0 * * * goaccess /usr/local/nginx/logs/access.log --output/usr/local/nginx/html/nginx-daily-report-$(date \%Y\%m\%d).html --log-formatCOMBINED

更多文章