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

Jul 04, 2023 am 10:12 AM
nginx log cutting Website log storage

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!

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 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)

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 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 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.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

See all articles