静态缓存功能设置
对图片、CSS、JS等静态资源设置缓存,减少重复请求和磁盘IO开销,提升访问速度。
编辑Nginx配置server段,添加静态资源缓存规则:
nginx server { 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; # 关闭静态资源访问日志,降低IO add_header Cache-Control "public, max-age=31536000"; # 缓存控制头 open_file_cache max=10000 inactive=30s; # 缓存文件元数据 } location / { index index.html index.htm; } # 默认首页 } |
配置生效:
bash nginx -t && nginx -s reload |
设置连接超时
快速回收无效连接,避免空闲连接占用系统资源,优化连接复用效率。
编辑Nginx配置http段:
nginx http { 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; # 向后端发送请求超时 } |
配置生效:
bash nginx -t && nginx -s reload |
日志切割
Nginx默认不自动切割日志,日志过大会占用大量磁盘空间,通过logrotate实现定时切割。
创建配置文件
创建logrotate配置文件,指定Nginx日志路径和切割规则:
bash vi /etc/logrotate.d/nginx |
ini /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 } |
测试与验证
手动测试切割效果,确认系统定时任务正常:
bash logrotate -f /etc/logrotate.d/nginx # 手动执行切割 cat /etc/cron.daily/logrotate # 确认定时任务存在 |
配置网页压缩
启用Gzip压缩,减少网页传输带宽,提升加载速度,兼容所有浏览器。
编辑Nginx配置http段:
nginx http { gzip on; # 启用Gzip压缩 gzip_min_length 1024; # 仅压缩大于1KB的文件,避免浪费CPU gzip_comp_level 3; # 压缩等级(1-9),3为平衡值 # 需压缩的文件类型 gzip_types text/plain text/css application/javascript application/json application/xml; gzip_vary on; # 告知客户端支持压缩 # 可选Brotli压缩(压缩率更高,需提前编译对应模块) } |
配置生效并验证:
bash nginx -t && nginx -s reload |
bash curl -I -H "Accept-Encoding: gzip" http://服务器IP # 响应头含Content-Encoding: gzip即生效 |
Nginx的深度监控
GoAccess 简介
开源轻量实时日志分析工具,无需依赖数据库,可直接解析Nginx日志,生成直观的HTML报告,支持实时监控访问量、状态码、请求耗时等核心指标,快速定位访问异常。
GoAccess安装
CentOS系统通过EPEL源安装,简单高效,安装后验证版本:
bash yum install -y epel-release # 安装EPEL源(默认无GoAccess软件源) yum install -y goaccess goaccess --version # 验证安装成功 |
配置中文环境
安装中文语言包,配置环境变量,确保GoAccess报告显示中文:
bash yum install -y langpacks-zh_CN # 安装中文语言包 echo "LANG=zh_CN.UTF-8" >> /etc/profile # 永久设置中文环境 source /etc/profile # 立即生效 echo $LANG # 验证,输出zh_CN.UTF-8即成功 |
GoAccess生成中文报告
支持实时报告(动态更新)和静态报告(固定快照),按需生成。
实时报告
实时解析日志,每秒更新数据,适合实时监控场景:
bash goaccess /usr/local/nginx/logs/access.log \ --real-time-html --output=/usr/local/nginx/html/nginx-real-time-report.html \ --log-format=COMBINED --html-report-title="Nginx实时访问监控报告" |
静态报告
生成日志固定快照,适合存档、分享,无需实时更新:
bash goaccess /usr/local/nginx/logs/access.log \ --output=/usr/local/nginx/html/nginx-static-report.html \ --log-format=COMBINED --html-report-title="Nginx访问日志静态报告" |
测试访问
确保Nginx正常运行,通过浏览器访问报告页面:
bash nginx # 确保Nginx启动 systemctl status nginx # 查看运行状态 |
访问地址:
实时报告:http://服务器IP/nginx-real-time-report.html
静态报告:http://服务器IP/nginx-static-report.html
注:无法访问需开放80端口,执行命令:firewall-cmd --permanent --add-port=80/tcp; firewall-cmd --reload。
定时生成静态报告
通过crontab定时任务,每日凌晨生成静态报告,便于日志存档:
bash crontab -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-format=COMBINED |