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

Linux system server, nginx log cutting and saving tutorial.

WBOY
Release: 2024-02-18 17:51:15
forward
1030 people have browsed it

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
Copy after login
  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
Copy after login
  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                 # 脚本结束}
Copy after login

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!

Related labels:
source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template