nginx日志相关指令(十二)

简述

众所周知,线上如果出现事故我们通常需要通过日志文件来分析并定位问题/修复问题.
在nginx中日志分为两种:
access_log : 访问日志,查看请求用户IP,浏览器信息及请求时间等信息; 
error_log  : 查看线上出错的具体信息; 

日志配置指令

  • access_log

  • log_format

  • open_log_file_cache

  • log_subrequest

  • error_log

  • log_not_found

access_log

access_log 与 log_format

access_log访问日志主要记录客户端的请求,客户端向服务端发起的每一次请求都记录在这里,
客户端IP,浏览器信息,referer,请求处理事件,请求的url
都可以在访问日志中得到。当然要具体记录那些信息,就可以通过log_format来控制.
Syntax:		access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log 	off;
Default:	access_log logs/access.log combined;
Context:	http, server, location, if in location, limit_except

# path 指定日志存放位置
# format 日志格式
# buffer 默认64k
# gzip 日志写入前先进行压缩 ; 1-9 ; 默认1; 压缩比越大,速度越慢
# flush 缓存有效时间
# if 条件判断是否写入日志
# off 如果指定该值,则当前作用域下所有日志都关闭
Syntax:	log_format name [escape=default|json|none] string ...;
Default:	
log_format combined "...";
Context:	http

方向代理时的格式化设置获取真实IP地址通过 $http_x_forwarded_for , 其他常用的变量如下

Open_log_file_cache

日志记录的写入是打开文件,写入记录,关闭文件;
如果日志文件存在变量,为提高性能,可以通过Open_log_file_cache缓存文件描述符

Syntax:	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

log_subrequest

启用禁用子请求是否写入访问日志access-log

Syntax:	log_subrequest on | off;
Default:	
log_subrequest off;
Context:	http, server, location

error_log

错误日志在nginx中通过error_log指令实现的,指令记录服务器和请求处理过程中的错误信息

error_log

Syntax:	error_log file [level];
Default:	
error_log logs/error.log error;
Context:	main, http, mail, stream, server, location

level: debug, info, notice, warn, error, crit, alert, or emerg.

 --with-debug 配置debug

log_not_found

启用禁用文件不存在是否写入文件

Syntax:	log_not_found on | off;
Default:	
log_not_found on;
Context:	http, server, location
原文地址:https://www.cnblogs.com/pengsn/p/13402138.html