本文介绍如何在 Debian 系统上配置 Nginx 日志轮转,主要利用 logrotate 工具。
步骤一:安装 logrotate
首先,确保系统已安装 logrotate:
sudo apt-get update sudo apt-get install logrotate
步骤二:创建或编辑 Nginx logrotate 配置文件
在 /etc/logrotate.d/ 目录下创建或编辑名为 nginx 的配置文件:
sudo nano /etc/logrotate.d/nginx
步骤三:添加配置文件内容
将以下内容添加到 nginx 文件中:
/var/log/nginx/*.log { daily missingok rotate 7 compress delaycompress notifempty create 0640 www-data adm sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 $(cat /var/run/nginx.pid) fi endscript }
参数说明:
步骤四:测试配置
使用以下命令测试配置:
sudo logrotate -d /etc/logrotate.d/nginx
-d 参数用于测试模式,不会实际执行轮转。 如有错误,会显示出来。
步骤五:验证定时任务
logrotate 通常由每日 cron 任务自动运行。您可以检查 /etc/cron.daily/logrotate 文件以确认:
cat /etc/cron.daily/logrotate
如果该文件存在且包含正确的 logrotate 命令,则日志轮转将每天自动执行。
完成以上步骤后,您的 Debian 系统上的 Nginx 日志将按照配置进行轮转。
以上就是如何设置Debian Nginx日志轮转的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号