Nginx 日志系统


  • nginx的日志配置有这几个命令模块access_log、log_format、open_log_file、log_not_found、log_subrequest、rewrite_log、error_log
  • nginx有一个非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志
1.log_format
  • log_format是用来设置日志格式
  • 配置段: http
  • 语法: log_format name formatStr;
  • name表示格式名称,不允许重复,formatStr表示等义的格式
  • log_format有一个默认的、无须设置的combined日志格式设置,相当于Apache的combined日志格式,其具体参数如下
log_format combined '$remote_addr-$remote_user [$time_local]'
‘"$request"$status $body_bytes_sent’
 ‘"$http_referer" "$http_user_agent"’
  • 日志格式允许包含的变量注释如下
    |参数|说明|例子|
    |-|-|
    | r e m o t e a d d r ∣ 客 户 端 地 址 ∣ 211.28.65.253 ∣ ∣ remote_addr|客户端地址 |211.28.65.253| | remoteaddr211.28.65.253remote_user| 客户端用户名称| |
    | t i m e l o c a l ∣ 访 问 时 间 和 时 区 ∣ 18 / J u l / 2012 : 17 : 00 : 01 + 0800 ∣ ∣ time_local|访问时间和时区|18/Jul/2012:17:00:01 +0800| | timelocal访18/Jul/2012:17:00:01+0800request|请求的URI和HTTP协议 |“GET /article-10000.html HTTP/1.1”|
    | h t t p h o s t ∣ 请 求 地 址 , 即 浏 览 器 中 你 输 入 的 地 址 ( I P 或 域 名 ) ∣ w w w . w a n g . c o m 192.168.100.100 ∣ ∣ http_host|请求地址,即浏览器中你输入的地址(IP或域名)|www.wang.com 192.168.100.100| | httphostIPwww.wang.com192.168.100.100status|HTTP请求状态|200|
    | u p s t r e a m s t a t u s ∣ u p s t r e a m 状 态 ∣ 200 ∣ ∣ upstream_status|upstream状态|200| | upstreamstatusupstream200body_bytes_sent| 发送给客户端文件内容大小 |1547|
    | h t t p r e f e r e r ∣ u r l 跳 转 来 源 ∣ h t t p s : / / w w w . b a i d u . c o m / ∣ ∣ http_referer|url跳转来源 |https://www.baidu.com/| | httprefererurlhttps://www.baidu.com/http_user_agent|用户终端浏览器等信息|"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;|
    | s s l p r o t o c o l ∣ S S L 协 议 版 本 ∣ T L S v 1 ∣ ∣ ssl_protocol|SSL协议版本|TLSv1| | sslprotocolSSLTLSv1ssl_cipher|交换数据中的算法|RC4-SHA|
    | u p s t r e a m a d d r ∣ 后 台 u p s t r e a m 的 地 址 , 即 真 正 提 供 服 务 的 主 机 地 址 ∣ 10.10.10.100 : 80 ∣ ∣ upstream_addr|后台upstream的地址,即真正提供服务的主机地址|10.10.10.100:80| | upstreamaddrupstream10.10.10.100:80request_time|整个请求的总时间 |0.205|
    |$upstream_response_time|请求过程中,upstream响应时间|0.002|
2.assess_log
  • access_log为访问日志,记录所有对nginx进行请求的访问
  • 语法:

access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off; # 不记录日志

  • 默认值: access_log logs/access.log combined;
  • 配置段: http, server, location, if in location, limit_except gzip压缩等级。
  • buffer设置内存缓存区大小。
  • flush保存在缓存区中的最长时间。
3.open_log_file_cache 指令
  • 可以使用open_log_file_cache来设置日志文件缓存,对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭
  • 语法:

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;

  • 参数注释如下:

  • max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。

  • inactive:设置存活时间,默认是10s

  • min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次 valid:设置检查频率,默认60s

  • off:禁用缓存

  • 默认值: open_log_file_cache off;

  • 配置段: http, server, location
    ####4.log_not_found指令

  • 是否在error_log中记录不存在的错误

  • 语法:

log_not_found on | off;

  • 默认值: log_not_found on;
  • 配置段: http, server, location
5.log_subrequest 指令
  • 是否在access_log中记录子请求的访问日志
  • 语法:

log_subrequest on | off;

  • 默认值: log_subrequest off;
  • 配置段: http, server, location
6.rewrite_log 指令
  • 由ngx_http_rewrite_module模块提供的。用来记录重写日志的。启用时将在error log中记录notice级别的重写日志。对于调试重写规则建议开启。
  • 语法:

rewrite_log on | off;

  • 默认值: rewrite_log off;
  • 配置段: http, server, location, if
7.error_log
  • 配置错误日志存放的位置及设置需要记录的级别的错误
  • 语法:

error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];

  • 默认值: error_log logs/error.log error;
  • 配置段: main, http, server, location
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值