When it comes to Nginx log cutting and saving, you can use the logrotate tool. Here's a simple tutorial to help you achieve this:
sudo apt-get install logrotate # Debian/Ubuntu sudo yum install logrotate # CentOS/RHEL
nginx-logrotate.conf
:sudo nano /etc/logrotate.d/nginx-logrotate.conf
/path/to/nginx/logs/*.log { daily # 每天切割日志文件 missingok # 如果日志文件不存在也不会报错 rotate 7 # 保留最近7个日志文件 compress # 压缩旧的日志文件 delaycompress # 延迟压缩,直到下次切割时才压缩 notifempty # 如果日志文件为空,则不切割 create 0644 <user> <group> # 设置新生成的日志文件的权限和所有者,用具体的用户和组替换<user>和<group> sharedscripts # 在所有日志文件都处理完毕后,运行一次脚本 postrotate # 切割后执行的命令 /usr/sbin/nginx -s reopen endscript # 脚本结束}
Please note that you need to replace /path/to/nginx/logs/
with the path to your actual Nginx logs directory and <user> Replace </user>
and <group></group>
with the appropriate user and group.
logrotate is now configured to cut Nginx log files every day and keep the most recent 7 log files. You can customize options in the configuration file according to your needs, such as the number of log files to retain and the cutting frequency.
To avoid excessive disk space being taken up, be sure to check and clean old log files regularly. You can use crontab or other scheduled task tools to run cleanup commands regularly, such as deleting old log files within a certain time range.
The above is the detailed content of Linux system server, nginx log cutting and saving tutorial.. For more information, please follow other related articles on the PHP Chinese website!