Home >Operation and Maintenance >Nginx >How to implement Nginx log cutting configuration
How to implement Nginx log cutting configuration requires specific code examples
Nginx is a very popular high-performance web server and reverse proxy server that is widely used for building and running modern applications. As an operation and maintenance engineer, we often need to configure the log cutting function of the Nginx server so that the server's logs can be cut regularly to prevent excessive log files from affecting the server's performance. The following will introduce how to implement Nginx log cutting configuration and give specific code examples.
1. Why do you need to configure log cutting?
Nginx server writes all access logs and error logs to the same file by default. As the number of visits increases, the log file will gradually become It is huge and not only takes up a lot of disk space, but also reduces efficiency when looking for specific log information. In order to solve this problem, we need to regularly cut Nginx logs, cut log files according to a certain size or time interval, and archive or delete old log files to maintain server performance and manageability.
2. How to configure Nginx log cutting
logrotate is a commonly used log cutting tool that can be used for various Log files are automatically cut, archived and deleted. First, we need to install the logrotate tool on the server. If your server uses a Debian/Ubuntu system, you can use the following command to install it:
sudo apt-get install logrotate
If your server uses a CentOS/RHEL system, you can use the following command to install it:
sudo yum install logrotate
Next, we need to create an Nginx log cutting configuration file to define the cutting rules and operations to be performed. Normally, Nginx's log files are located in the /var/log/nginx/
directory. We can create a file named nginx.logrotate
in this directory and edit it as follows Configuration:
/var/log/nginx/*.log { daily missingok rotate 7 compress delaycompress notifempty create 0644 nginx nginx sharedscripts postrotate /usr/sbin/nginx -s reopen endscript }
In the above configuration, the meaning of each parameter is as follows:
daily
: Cut according to the daily time interval; missingok
: If the log file does not exist, no error will be reported; rotate 7
: Keep the last 7 cut log files, and the old log files will be deleted ; compress
: Compress the log file after cutting; delaycompress
: Delay compression, that is, compression will be performed during the next cutting; notifempty
: If the cut log file is empty, no operation is performed; create 0644 nginx nginx
: Create a new log file, set permissions to 0644, owner and group to nginx; sharedscripts
: Execute the script only once before executing the cutting script; postrotate
and endscript
: Scripts executed after the cutting is completed to reopen the log file on the Nginx server. After completing the writing of the configuration file, you can use the following command to test the log cutting:
sudo logrotate -f /etc/logrotate.d/nginx.logrotate
Execute After the above command, logrotate will cut the Nginx log file according to the rules of the configuration file. You can use the ls
command to view the log files in the /var/log/nginx/
directory to see whether they have been cut and compressed according to the rules.
In order to realize the automatic log cutting function, we need to add the execution task of logrotate to the scheduled task of the system. You can edit the /etc/crontab
file and add the following line:
0 0 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/nginx.logrotate
In this way, logrotate will perform a log cutting operation at midnight (0 hours, 0 minutes) every day.
Summary
It is very necessary to configure the log cutting function of Nginx, which can maintain the performance and manageability of the server. By installing the logrotate tool, creating a log cutting configuration file, and setting up scheduled tasks, you can realize the automatic log cutting function. I hope the specific code examples provided in this article can help you implement Nginx log cutting configuration.
The above is the detailed content of How to implement Nginx log cutting configuration. For more information, please follow other related articles on the PHP Chinese website!