Nginx log format and three log cutting articles

A: the role of

  Ngx_http_log_module: custom log format, and saved in the specified format.

II: Example 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

   句法: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

       access_log off;
  Default: access_log logs / access.log merger;
  语境: http,server,location,if in location,limit_except

  1:[buffer=size]

  Set path buffer log writes, the format and configuration.

  2:gzip[=level]

  If this parameter gzip, before writing the file, the data buffer will be compressed. 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, the compression level is set to 1. Because the data is compressed atomic block, so the log file can be read by the extract or "zcat" at any time.

  3:[flush=time] 

  Stored in the cache area of ​​the maximum time.

Four: log_format

  Specifies 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 record the client's IP address

  2: remote_user record client user name
  3: request records the URL and HTTP request
  4: status record request status
  5: body_bytes_sent number of bytes sent to the client, the response does not include the size of the head; the variable in the Apache module mod_log_config "% B" compatible parameters.
  6: bytes_sent total number of bytes sent to the client.
  7: connection sequence number for the connection.
  8: connection_requests the current number of requests by a connection obtained.
  9: msec log write time. Seconds, milliseconds is.
  10: pipe if the HTTP request is sent via line (pipelined), pipe is "p", otherwise. "."
  11: http_referer record access which came from pages link
  12: http_user_agent record information about the client browser
  13: length request_length request (including the request line, request headers and body of the request).
  14: request_time request processing time, in seconds, milliseconds accuracy; starting from the first byte read client performed so far until the log is written to the last character transmitted after the client.
  15: time_iso8601 ISO8601 local time in standard format.
  16: the local time at time_local common log format.

 

 

五:open_log_file_cache

  句法: 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

  Action: definition of a buffer, for storing the log file descriptors common name contained in the variable. The instruction has the following parameters:

  max: set the maximum number of buffer descriptors; if the cache becomes full, the least recently used (LRU) Close descriptor
  inactive: provided after this time if there is no cache access time descriptor is closed; default is 10 seconds
  min_uses: using the minimum number of parameters defined in the inactive time setting file so that the descriptor cache maintained in the open state; default, 1
  valid: Set time should check whether the file still exists with the same name; default is 60 seconds
  off: Disable Caching

  Example usage:

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


Six: log cutting

1. Define the log roll round Strategy

# 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
}

[Warning] / data / weblogs / *. Log when using wildcards / all log files at the matched data / weblogs / directory will be cut. If you want to cut a specific log file, it is assigned to the file. [/ Warning]

2. Set Scheduled Tasks

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

Such daily 23:59 minutes execution log cutting.

 

Guess you like

Origin www.cnblogs.com/tanxiaojun/p/12152920.html