zoukankan      html  css  js  c++  java
  • nginx日志模块及日志定时切割

    一:作用

      Ngx_http_log_module:定义日志格式,并且以指定的格式保存。

    二:示例配置

    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;

    三:access_log

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

           access_log off;
      默认: access_log logs / access.log合并;
      语境: http,server,location,if in location,limit_except

      1:[buffer=size]

      设置缓冲日志写入的路径,格式和配置。

      2:gzip[=level]

      如果使用该gzip参数,则在写入文件之前,缓冲的数据将被压缩。压缩级别可以设置在1(最快,较少压缩)和9(最慢,最佳压缩)之间。默认情况下,缓冲区大小等于64K字节,压缩级别设置为1.由于数据是以原子块压缩的,因此日志文件可以随时解压或由“ zcat” 读取

      3:[flush=time] 

      保存在缓存区中的最长时间。

    四: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 记录客户端IP地址

      2:remote_user 记录客户端用户名称
      3:request 记录请求的URL和HTTP协议
      4:status 记录请求状态
      5:body_bytes_sent 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
      6:bytes_sent 发送给客户端的总字节数。
      7:connection 连接的序列号。
      8:connection_requests 当前通过一个连接获得的请求数量。
      9:msec 日志写入时间。单位为秒,精度是毫秒。
      10:pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
      11:http_referer 记录从哪个页面链接访问过来的
      12:http_user_agent 记录客户端浏览器相关信息
      13:request_length 请求的长度(包括请求行,请求头和请求正文)。
      14:request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
      15:time_iso8601 ISO8601标准格式下的本地时间。
      16:time_local 通用日志格式下的本地时间。

    五:open_log_file_cache

      句法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
         open_log_file_cache off;
      默认:
         open_log_file_cache off;
      语境: http,server,location

      作用:定义一个缓存,用于存储名称中包含变量的常用日志的文件描述符。该指令具有以下参数:

      max:设置缓存中描述符的最大数量; 如果缓存变满,则最近最少使用(LRU)描述符关闭
      inactive:设置在此时间之后如果没有访问时缓存的描述符被关闭的时间; 默认情况下为10秒
      min_uses:inactive参数定义的时间内设置文件使用的最小数量,以使描述符在缓存中保持打开状态; 默认情况下,1
      valid:设置应检查文件是否仍然存在同名的时间; 默认情况下为60秒
      off:禁用缓存

      用法示例:

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

    六:日志切割

    1. 定义日志轮滚策略

    # 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使用通配符时,/data/weblogs/目录下的所有匹配到的日志文件都将切割。如果要切割特定日志文件,就指定到该文件。[/warning]

    2. 设置计划任务

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

    这样每天23点59分钟执行日志切割。

  • 相关阅读:
    【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
    android studio 编程中用到的快捷键
    java时间格式串
    android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
    linux安装vmware
    x1c 2017 安装mint18的坑——grub2
    x1c2017 8G版 win linux的取舍纠结记录
    python的try finally (还真不简单)
    kafka+docker+python
    json文件不能有注释
  • 原文地址:https://www.cnblogs.com/yaohong/p/9094951.html
Copyright © 2011-2022 走看看