Nginx - 学习笔记 - 自定义日志输出格式&查看日志
- 配置参考
- 内置变量参考
- 查看日志文件
- 1. 先找配置文件
- 2. 再查看日志设置
- 3. 访问日志
- 筛选日志内容
- 参考资料
配置参考
...
http {
# 自定义日志输出格式
log_format aka_logs
'{"@timestamp":"$time_iso8601",'
'"host":"$hostname",'
'"server_ip":"$server_addr",'
'"client_ip":"$remote_addr",'
'"xff":"$http_x_forwarded_for",'
'"domain":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"args":"$args",'
'"upstreamtime":"$upstream_response_time",'
'"responsetime":"$request_time",'
'"request_method":"$request_method",'
'"status":"$status",'
'"size":"$body_bytes_sent",'
'"request_body":"$request_body",'
'"request_length":"$request_length",'
'"protocol":"$server_protocol",'
'"upstreamhost":"$upstream_addr",'
'"file_dir":"$request_filename",'
'"http_user_agent":"$http_user_agent"'
'}';
# 指定日志输出位置 /var/log/nginx/access.log
access_log /var/log/nginx/access.log aka_logs;
...
}
内置变量参考
$args #请求中的参数值
$query_string #同 $args
$arg_NAME #GET请求中NAME的值
$is_args #如果请求中有参数,值为"?",否则为空字符串
$uri #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"。
$document_uri #同 $uri
$document_root #当前请求的文档根目录或别名
$host #优先级:HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名.请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称
$hostname #主机名
$https #如果开启了SSL安全模式,值为"on",否则为空字符串。
$binary_remote_addr #客户端地址的二进制形式,固定长度为4个字节
$body_bytes_sent #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$bytes_sent #传输给客户端的字节数
$connection #TCP连接的序列号
$connection_requests #TCP连接当前的请求数量
$content_length #"Content-Length" 请求头字段
$content_type #"Content-Type" 请求头字段
$cookie_name #cookie名称
$limit_rate #用于设置响应的速度限制
$msec #当前的Unix时间戳
$nginx_version #nginx版本
$pid #工作进程的PID
$pipe #如果请求来自管道通信,值为"p",否则为"."
$proxy_protocol_addr #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
$realpath_root #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
$remote_addr #客户端地址
$remote_port #客户端端口
$remote_user #用于HTTP基础认证服务的用户名
$request #代表客户端的请求地址
$request_body #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
$request_body_file #将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传 递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off,uwsgi_pass_request_body off,or scgi_pass_request_body off
$request_completion #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
$request_filename #当前连接请求的文件路径,由root或alias指令与URI请求生成
$request_length #请求的长度 (包括请求的地址,http请求头和请求主体)
$request_method #HTTP请求方法,通常为"GET"或"POST"
$request_time #处理客户端请求使用的时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$request_uri #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
$scheme #请求使用的Web协议,"http" 或 "https"
$server_addr #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
$server_name #服务器名
$server_port #服务器端口
$server_protocol #服务器的HTTP版本,通常为 "HTTP/1.0" 或 "HTTP/1.1"
$status #HTTP响应代码
$time_iso8601 #服务器时间的ISO 8610格式
$time_local #服务器时间(LOG Format 格式)
$cookie_NAME #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
$http_NAME #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
$http_cookie
$http_host #请求地址,即浏览器中你输入的地址(IP或域名)
$http_referer #url跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent #用户终端浏览器等信息
$http_x_forwarded_for
$sent_http_NAME #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location
$sent_http_transfer_encoding
查看日志文件
1. 先找配置文件
/etc/nginx/nginx.conf
- 先用
ps
显示当前进程。
ps -aux | grep nginx
ps -aux | grep nginx
root 20316 0.0 0.0 50024 3264 ? Ss Jan19 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root 22168 0.0 0.0 115928 1020 pts/0 S+ 11:39 0:00 grep --color=auto nginx
nginx 24633 0.0 0.4 83240 35028 ? S Apr12 1:01 nginx: worker process
nginx 24634 0.0 0.4 82200 34144 ? S Apr12 5:31 nginx: worker process
nginx 24635 0.0 0.4 80780 32688 ? S Apr12 2:18 nginx: worker process
nginx 24636 0.0 0.4 81072 33008 ? S Apr12 5:32 nginx: worker process
- 不行的话从根目录
/
开始搜索。(如果可以尽量缩小范围,更快点)
find / | grep nginx.conf
/etc/nginx/conf.d
/etc/nginx/nginx.conf.default
/etc/nginx/nginx.conf
2. 再查看日志设置
cat /etc/nginx/nginx.conf
配置到手,日志的路径
和格式
都知道了。
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
...
3. 访问日志
实时追踪显示日志:
tail -f /var/log/nginx/access.log
然后就可以开始愉快的找问题啦。
筛选日志内容
除了实时分析,经常还要排查历史找线索。比如先到日志目录/var/log/nginx/
去看看。
[root@localhost nginx]# ll
total 60
-rw-r----- 1 root root 1744 Jul 17 11:51 access.log
-rw-r--r-- 1 root root 305 May 30 14:51 access.log-20220531.gz
-rw-r----- 1 root root 94 Jul 7 14:53 access.log-20220708.gz
-rw-r----- 1 root root 1330 Jul 11 13:15 access.log-20220712
-rw-r----- 1 root root 916 Jul 17 11:51 error.log
-rw-r----- 1 root root 188 Jul 4 02:56 error.log-20220704.gz
-rw-r----- 1 root root 237 Jul 5 12:57 error.log-20220706.gz
-rw-r----- 1 root root 265 Jul 9 17:21 error.log-20220710.gz
-rw-r----- 1 root root 248 Jul 10 14:02 error.log-20220711.gz
-rw-r----- 1 root root 357 Jul 11 13:15 error.log-20220712.gz
-rw-r----- 1 root root 213 Jul 13 00:12 error.log-20220713.gz
-rw-r----- 1 root root 188 Jul 13 23:56 error.log-20220714.gz
-rw-r----- 1 root root 223 Jul 14 22:04 error.log-20220715.gz
-rw-r----- 1 root root 190 Jul 15 14:17 error.log-20220716.gz
-rw-r----- 1 root root 493 Jul 16 18:35 error.log-20220717
- 日期区间筛选:匹配
2022年1月1日 12:30 —— 12:59
区间的日志。输出到新文件temp.log
grep '2022-01-01T12:[3-5][0-9]' access.log > temp.log
sz temp.log # 有需要的话可以下载日志文件,拿到本地来折腾。
- 条件或:匹配
url...
或userId
两个条件。
grep -E '"url":"/event/list"|userId=' temp.log
- 条件与:多次过滤,实现
and
grep '"url":"/event/list"' temp.log | grep 'userId'