Home > Operation and Maintenance > Nginx > Nginx log cutting configuration analysis, management of website log storage

Nginx log cutting configuration analysis, management of website log storage

王林
Release: 2023-07-04 10:12:06
Original
1402 people have browsed it

Nginx log cutting configuration analysis, management website log storage

During the operation of a website, logs are very important. It can provide detailed records of the running status of the website, helping developers and administrators analyze problems and optimize performance. However, as the website continues to grow, the log files will become larger and larger, which will tax the storage space and performance of the server. In order to solve this problem, we can use the log cutting function of Nginx to split the log files according to time or size, so as to achieve effective management and storage of logs.

Nginx is a high-performance web server, and its functions and behavior can be flexibly adjusted through configuration files. Below, we will use a simple example to demonstrate how to configure Nginx to implement log cutting.

First, we need to specify the log format and storage path in the Nginx configuration file. In Nginx's http module, you can define the log format by adding the following code:

http {
    ...
    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    ...
}
Copy after login

In the above code, the log_format directive defines a file named access Log format, which contains some commonly used log variables, such as client IP address, access time, request URL, return status code, etc.

Next, we need to apply this log format to the specific access log. In the server block of Nginx, you can specify the storage path and format of the access log by adding the following code:

server {
    ...
    access_log /var/log/nginx/access.log access;
    ...
}
Copy after login

In the above code, the access_log directive specifies Access log storage path and format. Among them, /var/log/nginx/access.log is the storage path of the log file, and access is the name of the previously defined log format.

By default, Nginx will write all access logs to the same file. However, when this file becomes very large, we may want to split it into multiple smaller files. In order to achieve this function, we can use the logrotate tool provided by Nginx.

logrotate is a commonly used log cutting tool that can split log files according to specified rules. We can write a configuration file named nginx, which defines the splitting rules of Nginx access logs. The following is an example:

/var/log/nginx/access.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    postrotate
        /usr/sbin/nginx -s reopen
    endscript
}
Copy after login

In the above code, /var/log/nginx/access.log is the path of the log file that needs to be split. daily specifies splitting by day, rotate 7 means retaining log files for 7 days. missingok means that if the log file does not exist, no error will be reported. notifempty means that if the log file is empty, no error will be reported. compress means to compress the newly generated log file. The code between postrotate and endscript will be executed after the log file is cut. Here, use /usr/sbin/nginx -s reopen to notify Nginx to reopen. Log files.

Finally, we need to place this nginx configuration file in the /etc/logrotate.d/ directory. logrotate will scan this directory regularly and then cut the logs according to the configuration files in it.

The above is a simple example of using Nginx to implement log cutting. By properly configuring Nginx's log format and cutting rules, we can effectively manage and store the website's access logs. This not only saves storage space but also improves the overall performance of the server. Hope this article is helpful to you.

The above is the detailed content of Nginx log cutting configuration analysis, management of website log storage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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