Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of nginx Log Module and Log timing cutting

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of nginx log module and log timing cutting. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

One: function

Ngx_http_log_module: defines the log format and saves it in the specified format.

Two: sample configuration

Log_format compression'$remote_addr-$remote_user [$time_local]'"$request" $status $bytes_sent''"$http_referer"$http_user_agent"$gzip_ratio"; access_log / spool/logs/nginx-access.log compression buffer=32k

Three: access_log

Syntax: access_log path [format [buffer=size] [gzip [= level]] [flush=time] [if=condition]]

Access_log off

Default: access_log logs / access.log merge

Context: http,server,location,if in location,limit_except

1: [buffer=size]

Set the path, format, and configuration of buffered log writes.

2:gzip [= level]

If you use this gzip parameter, the buffered data will be compressed before writing to the file. The compression level can be set between 1 (fastest, less compression) and 9 (slowest, best compression). By default, the buffer size is equal to 64K bytes and the compression level is set to 1. Because the data is compressed in atomic blocks, log files can be unzipped or read by "zcat" at any time.

3: [flush=time]

The maximum amount of time saved in the cache.

Four: log_format

Specify log format

Log_format compression'$remote_addr-$remote_user [$time_local]'"$request" $status $bytes_sent''"$http_referer"$http_user_agent"$gzip_ratio"'

1:remote_addr, $http_x_forwarded_for record client IP address

2:remote_user record client user name

3:request records the requested URL and HTTP protocols

4:status record request status

The number of bytes sent by 5:body_bytes_sent to the client, excluding the size of the response header; this variable is compatible with the "% B" parameter in the Apache module mod_log_config.

The total number of bytes sent by 6:bytes_sent to the client.

The serial number of the 7:connection connection.

The number of requests currently obtained by 8:connection_requests through one connection.

9:msec log write time. The unit is seconds and the precision is milliseconds.

10:pipe if the request is sent through the HTTP pipeline (pipelined), the pie value is "p", otherwise it is ".".

Which page is the 11:http_referer record accessed from?

12:http_user_agent records information about client browsers

The length of the 13:request_length request (including the request line, request header, and request body).

14:request_time request processing time, in seconds, precision milliseconds; from reading the first byte of the client to log writing after the last character is sent to the client.

Local time in 15:time_iso8601 ISO8601 standard format.

The local time under the 16:time_local universal log format.

Five: open_log_file_cache

Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]

Open_log_file_cache off

Default:

Open_log_file_cache off

Context: http,server,location

Purpose: define a cache to store file descriptors for commonly used logs with variables in their names. The directive has the following parameters:

Max: sets the maximum number of descriptors in the cache; if the cache becomes full, use the least recent (LRU) descriptor to close

Inactive: sets the time when the cached descriptor is turned off if there is no access after this time; 10 seconds by default

Min_uses: sets the minimum number of files used within the time defined by the inactive parameter to keep the descriptor open in the cache; by default, 1

Valid: sets the time to check whether a file with the same name still exists; 60 seconds by default

Off: disable caching

Usage example:

Open_log_file_cache max = 1000 inactive = 20s valid = 1m min_uses = 2

Six: log cutting

1. Define log roll policy

# vim nginx-log-rotate/data/weblogs/*.log {nocompress daily copytruncate create notifempty rotate 7 olddir / data/weblogs/old_log missingok dateext postrotate / bin/kill-HUP `cat / var/run/nginx.pid 2 > / dev/ null`2 > / dev/null | | true endscript}

When [warning] / data/weblogs/*.log uses wildcards, all matching log files in the / data/weblogs/ directory will be cut. If you want to cut a specific log file, specify to that file. [/ warning]

two。 Set up scheduled tasks

59 23 * root (/ usr/sbin/logrotate-f / PATH/TO/nginx-log-rotate)

Log cutting is performed at 23: 59 minutes every day.

On the "nginx log module and log timing cutting example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report