nginx的access.log 和 error.log

nginx 常用的配置文件有两种: access.log 和 error.log

access.log 的作用是 记录用户所有的访问请求,不论状态码,包括200 ,404,500等请求,404,500的请求并不会出现在error.log中。

error.log 的作用是 记录nginx 本身运行时的一些错误,不会记录用户访问的请求。比如记录模块错误信息日志,以及nginx配置文件的错误日志等,格式不支持自定义,可以设置级别。

access.log 的格式设置:

log_format  combined  '$remote_addr - $remote_user  [$time_local]  '
                      ' "$request"  $status  $body_bytes_sent  '
                      ' "$http_referer"  "$http_user_agent" ';

#日志格式允许包含的变量注释如下:

$remote_addr, $http_x_forwarded_for 记录客户端IP地址
$remote_user 记录客户端用户名称
$request 记录请求的URL和HTTP协议
$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 通用日志格式下的本地时间。

#参考实例

http {
	log_format main '$remote_addr - $remote_user [$time_local] "$request" '
			'"$status" $body_bytes_sent "$http_referer" '
			'"$http_user_agent" "$http_x_forwarded_for" '
			'"$gzip_ratio" $request_time $bytes_sent $request_length';

	log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
				    '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
				    '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

	open_log_file_cache max=1000 inactive=60s;

	server {
	    server_name ~^(www\.)?(.+)$;
	    access_log logs/$2-access.log main;
	    error_log logs/$2-error.log;

	    location /srcache {
		access_log logs/access-srcache.log srcache_log;
	    }
	}
}

error.log 配置示例:
#错误日志保存位置
#error_log logs/error.log;
#指定错误日志的位置和级别
#error_log logs/error.log notice;
#error_log logs/error.log info;

posted @ 2020-02-14 18:27  enumx  阅读(264)  评论(0编辑  收藏  举报