Home  >  Article  >  Computer Tutorials  >  Linux system server, nginx log cutting and saving tutorial.

Linux system server, nginx log cutting and saving tutorial.

WBOY
WBOYforward
2024-02-18 17:51:151010browse

Linux system server, nginx log cutting and saving tutorial.

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:

  1. First, make sure you have installed the logrotate tool. On most Linux distributions, you can install it using the following command:
sudo apt-get install logrotate  # Debian/Ubuntu
sudo yum install logrotate      # CentOS/RHEL
  1. Create a configuration file for Nginx logs. In the terminal, use a text editor (such as vi or nano) to create a new file, such as
    nginx-logrotate.conf
sudo nano /etc/logrotate.d/nginx-logrotate.conf
  1. In the opened configuration file, enter the following:
/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.

  1. Save and close the configuration file.

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!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete