/var/log/nginx/
,通过修改/etc/nginx/nginx.conf
可自定义日志格式和路径,如设置log_format
和access_log
、error_log
指令。logrotate
工具,编辑/etc/logrotate.d/nginx
配置文件,设置按天(daily
)、保留天数(rotate
)、压缩(compress
)等参数,例如:/var/log/nginx/*.log {
daily
rotate 30
compress
delaycompress
missingok
create 0640 nginx nginx
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
配置后通过logrotate -d /etc/logrotate.d/nginx
测试,logrotate
默认通过cron.daily
每日自动执行。tail -f
实时查看日志,或multitail
同时监控多个日志文件并高亮关键信息。GoAccess
生成访问日志的实时分析报告,或使用ELK Stack
(Elasticsearch+Logstash+Kibana)实现日志的可视化分析。logrotate
配置中通过maxage
或minsize
参数设置过期时间,或通过cron
定时任务执行清理脚本,删除超过指定天数(如30天)的日志文件。scp
或rsync
实现异地备份。750
,Nginx用户(如nginx
)拥有写入权限,避免未授权访问。