nginx log module and log timing cutting method

  • 2020-05-14 06:09:17
  • OfStack

1: the role of

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

2: 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;

3: 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]

Sets the path, format, and configuration of buffer log writes.

2: gzip [= level]

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

3: [flush = time]

The longest time stored in the cache.

4: log_format

Specify the 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 records the client IP address

2: remote_user records the client user name

3: request records the requested URL and HTTP protocols

4: status records the status of the request

5: the number of bytes body_bytes_sent sent to the client, not including the size of the response header; This variable is compatible with the '%B' parameter in the Apache module mod_log_config.

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

7: serial number of connection connection.

8: connection_requests current number of requests received over 1 connection.

9: write time of msec log. The units are seconds and the accuracy is milliseconds.

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

11: http_referer records which page link was accessed from

12: http_user_agent records information about the client browser

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

14: request_time request processing time, in seconds, precision milliseconds; Start with the first byte read into the client until the last character is sent to the client and the log is written.

15: local time in time_iso8601 ISO8601 standard format.

16: local time in time_local common log format.

5: open_log_file_cache

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

open_log_file_cache off;

The default:

open_log_file_cache off;

Context: http, server, location

What it does: define a cache to store file descriptors for commonly used logs with variables in the name. This instruction has the following parameters:

max: sets the maximum number of descriptors in the cache; If the cache is full, the least recently used (LRU) descriptor is closed inactive: sets the time after this time when the cached descriptor is turned off if there is no access; The default is 10 seconds min_uses: sets the minimum number of files to be used within the time defined by the inactive parameter to keep the descriptor open in the cache; By default, 1 valid: setting should check whether the file still has the same name at the time; The default is 60 seconds off: disable caching

Usage examples:


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

6: log cutting

1. Define a 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 log files in the /data/weblogs/ directory are cut. If you want to cut a particular log file, you specify that file. [/ warning]

2. Set scheduled tasks


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

Log cutting is performed at 23.59 minutes per day.


Related articles: