/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)拥有写入权限,避免未授权访问。