Home Operation and Maintenance Nginx How to implement Nginx log cutting configuration

How to implement Nginx log cutting configuration

Nov 08, 2023 pm 12:48 PM
nginx log cutting configuration

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

  1. Install logrotate tool

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
Copy after login

If your server uses a CentOS/RHEL system, you can use the following command to install it:

sudo yum install logrotate
Copy after login
  1. Create log cutting configuration file

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
}
Copy after login

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.
  1. Test log cutting configuration

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
Copy after login

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.

  1. Set the scheduled task of logrotate

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
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Apr 13, 2025 pm 11:15 PM

Apple's iPhone 17 may usher in a major upgrade to cope with the impact of strong competitors such as Huawei and Xiaomi in China. According to the digital blogger @Digital Chat Station, the standard version of iPhone 17 is expected to be equipped with a high refresh rate screen for the first time, significantly improving the user experience. This move marks the fact that Apple has finally delegated high refresh rate technology to the standard version after five years. At present, the iPhone 16 is the only flagship phone with a 60Hz screen in the 6,000 yuan price range, and it seems a bit behind. Although the standard version of the iPhone 17 will have a high refresh rate screen, there are still differences compared to the Pro version, such as the bezel design still does not achieve the ultra-narrow bezel effect of the Pro version. What is more worth noting is that the iPhone 17 Pro series will adopt a brand new and more

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

How to start nginx How to start nginx Apr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to solve nginx encounter ddos How to solve nginx encounter ddos Apr 14, 2025 pm 12:12 PM

Nginx's DDoS attacks can be dealt with by identifying attack types, mitigating attacks, protecting Nginx configuration, monitoring and response, and working with service providers. Specific steps include enabling rate limiting, using WAF and CDN, updating Nginx, encrypting with TLS/SSL, monitoring logs, establishing an alert system, developing a contingency plan, and contacting a hosting provider and reporting to the authorities.

See all articles