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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

Nginx Performance Tuning: Optimizing for Speed and Low Latency Nginx Performance Tuning: Optimizing for Speed and Low Latency Apr 05, 2025 am 12:08 AM

Nginx performance tuning can be achieved by adjusting the number of worker processes, connection pool size, enabling Gzip compression and HTTP/2 protocols, and using cache and load balancing. 1. Adjust the number of worker processes and connection pool size: worker_processesauto; events{worker_connections1024;}. 2. Enable Gzip compression and HTTP/2 protocol: http{gzipon;server{listen443sslhttp2;}}. 3. Use cache optimization: http{proxy_cache_path/path/to/cachelevels=1:2k

How do I configure Nginx for server-side includes (SSI)? How do I configure Nginx for server-side includes (SSI)? Mar 17, 2025 pm 05:06 PM

The article discusses configuring Nginx for server-side includes (SSI), performance implications, using SSI for dynamic content, and troubleshooting common SSI issues in Nginx.Word count: 159

How do I implement HTTP authentication (basic auth, digest auth) in Nginx? How do I implement HTTP authentication (basic auth, digest auth) in Nginx? Mar 17, 2025 pm 05:03 PM

The article discusses implementing HTTP authentication in Nginx using basic and digest methods, detailing setup steps and security implications. It also covers using authentication realms for user management and suggests combining authentication meth

How do I configure Nginx for URL rewriting and redirection? How do I configure Nginx for URL rewriting and redirection? Mar 17, 2025 pm 05:02 PM

The article discusses configuring Nginx for URL rewriting and redirection, detailing steps and best practices. It addresses common mistakes and testing methods to ensure effective URL management.

How do I monitor Nginx performance and resource usage? How do I monitor Nginx performance and resource usage? Mar 17, 2025 pm 05:08 PM

The article discusses monitoring and optimizing Nginx performance, focusing on using tools like Nginx's status page, system-level monitoring, and third-party solutions like Prometheus and Grafana. It emphasizes best practices for performance optimiza

What are the best tools for monitoring Nginx? What are the best tools for monitoring Nginx? Mar 17, 2025 pm 05:09 PM

The article discusses top Nginx monitoring tools like Datadog, New Relic, and NGINX Amplify, focusing on their features for real-time monitoring, alerting, and detailed metrics to enhance server performance.

How do I configure Gzip compression in Nginx? How do I configure Gzip compression in Nginx? Mar 17, 2025 pm 04:57 PM

The article details how to configure Gzip compression in Nginx, its performance benefits, and verification methods. Main issue: optimizing web server performance through compression.[159 characters]

How do I configure Nginx for WebSocket proxying? How do I configure Nginx for WebSocket proxying? Mar 17, 2025 pm 05:01 PM

Article discusses configuring Nginx for WebSocket proxying, detailing necessary settings and troubleshooting steps for successful WebSocket connections.(159 characters)

See all articles