nginx之日志配置

参考:https://www.cnblogs.com/biglittleant/p/8979856.html

access_log

  • access_log用来定义日志级别,日志位置。语法如下:
    日志级别: debug > info > notice > warn > error > crit > alert > emerg
语法格式:	access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
                     access_log off;
默认值   :	access_log logs/access.log combined;
作用域   :	http, server, location, if in location, limit_except
  • 示例一
access_log /spool/logs/nginx-access.log compression buffer=32k;

log_format定义日志格式

语法格式:	log_format name [escape=default|json] string ...;
默认值    :	log_format combined "...";
作用域    :	http
  • 示例一
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;

常见的日志变量

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

open_log_file_cache

  • 将多个日志进行积累,达到一定量级后写入到磁盘,可以减少磁盘旋转,从而降低磁盘i/o,提升nginx能效
  • 使用open_log_file_cache来设置日志文件缓存(默认是off)。
    • max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭
    • inactive:设置存活时间,默认是10s
    • min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
    • valid:设置检查频率,默认60s
    • off:禁用缓存
语法格式:	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
  • 示例一
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
posted @ 2023-09-13 09:41  hasome  阅读(3)  评论(0编辑  收藏  举报