Nginx 日志格式配置介绍
Nginx日志格式配置介绍
by:授客 QQ:1033553122
测试环境
CentOS 6.5-x86_64
nginx-1.10.0
配置例子
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_time $upstream_response_time $request_length $bytes_sent $body_bytes_sent $gzip_ratio $connection_requests "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/nginx-access.log main;
说明:如下,有时候我们可以把日志配置json串格式,方便其它程序使用
log_format main '{
"remote_addr":"$remote_addr",
"remote_user":"$remote_user",
"time_local":"$time_local",
"request":"$request",
"status":"$status",
"request_time":"$request_time",
"upstream_response_time":"$upstream_response_time",
"request_length":"$request_length",
"bytes_sent":"$bytes_sent",
"body_bytes_sent":"$body_bytes_sent",
"gzip_ratio":"$gzip_ratio",
"connection_requests":"$connection_requests",
"http_referer":"$http_referer",
"http_user_agent":"$http_user_agent",
"http_x_forwarded_for":"$http_x_forwarded_for"
}';
配置说明
可在相同层级(个人理解:这里的层级为下文的Context范围)指定多个日志
语法说明:
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必须存在,如果开启了gzip日志压缩,则不能通过控制台实时查看日志了。
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http
format变量说明
$remote_addr
发起请求的客户端所在ip地址
$remote_user
发起请求的客户端用户名称,获取不到则显示为 -
$time_local
用来记录访问时间与时区(依赖nginx服务器本地时间),形如 20/Aug/2017:21:15:19 +0800,获取不到则显示为 -
$time_iso8601
类似$time_local,不同的是这里采用ISO 8601标准格式
$request
记录发起的请求,形如
POST /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1
$status
记录响应状态,比如 200
$request_time
记录请求处理时间(以秒为单位,携带毫秒的解决方案),从读取客户端第一个字节开始算起,到发送最后一个字节给客户端的时间间隔(原文:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client)
$upstream_response_time
记录nginx从后端服务器(upstream server)获取响应的时间(以秒为单位,携带毫秒的解决方案),多个请求的时间以逗号分隔
参考链接:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_response_time
$request_length
记录请求长度(包括请求行,请求头,请求体)
$gzip_ratio
记录nginx gzip压缩比例,获取不到则显示为 -
$bytes_sent
发送给客户端的字节数
$body_bytes_sent
发送给客户端的响应体字节数
$connection_requests
单个连接的并发请求数(the current number of requests made through a connection (1.1.18)
$http_referer
记录请求引用页面地址
$http_user_agent
记录用户代理信息(通常是浏览器信息
$http_x_forwarded_for
当为了承受更大的负载使用反向代理时,web服务器不能获取真实的客户端IP,$remote_addr获取到的是反向代理服务器的ip,这种情况下,代理服务器通常会增加一个叫做x_forwarded_for的信息头,把连接它的真实客户端IP加到这个信息头里,这样就能保证网站的web服务器能获取到真实IP,获取不到则显示为 -
$connection
连接序列号
$msec
写入日志的时间(以秒为单位,携带毫秒的解决方案)(原文:time in seconds with a milliseconds resolution at the time of the log write)
$pipe
如果为管道请求则显示为p,否则显示为 .
日志输出样例
# tail -f /usr/local/ngnix/logs/access.log
192.168.1.101 - - [20/Aug/2017:22:28:44 +0800] "POST /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1" 200 0.365 0.365 764 794 302 - 1 "http://192.168.1.102/zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0" "-"
# tail -f /usr/local/ngnix/logs/access.log
{
"remote_addr":"192.168.1.101",
"remote_user":"-",
"time_local":"31/Aug/2017:10:37:01 +0800",
"request":"GET /zentaopms/www/index.php?m=user&f=login&referer=L3plbnRhb3Btcy93d3cvaW5kZXgucGhw HTTP/1.1",
"status":"200",
"request_time":"0.562",
"upstream_response_time":"0.562",
"request_length":"546",
"bytes_sent":"8013",
"body_bytes_sent":"7520",
"gzip_ratio":"-",
"connection_requests":"1",
"http_referer":"-",
"http_user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
"http_x_forwarded_for":"-"
}
参考链接:
http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
Nginx 日志格式配置介绍的更多相关文章
- nginx日志格式配置
我一向对日志这个东西有些许恐惧,因为在分析日志是需要记住不同服务器日志的格式,就拿提取ip这一项来说,有的服务器日志是在第一列,有的是第二列或则第三列等等.知道今天我才发现,日志格式是可以自定义配置的 ...
- 运维技巧-Nginx日志格式
1.说一说 当你安装完nginx,输出的格式是比较乱的,这样我们就需要自己去定义一下,自己看着舒服的格式. 2.Nginx日志字段 $remote_addr 记录客户端IP,但她的值不是客户端提供的, ...
- Nginx日志格式以及相关配置
一.Nginx日志格式以及参数说明log_format main '$remote_addr - $remote_user [$time_local] "$request" ' ...
- Nginx日志文件配置与切割
Nginx日志的指令主要有两条: log_format,设置日志的格式 access_log,指定日志文件的存放路径.格式和缓存大小 两条指令在Nginx配置文件中的位置可以在http{……..}之间 ...
- ELK 之三:Kibana 使用与Tomcat、Nginx 日志格式处理
一:kibana安装: kibana主要是搜索elasticsearch的数据,并进行数据可视化的展现,新版使用nodejs. 1.下载地址: https://www.elastic.co/downl ...
- nginx日志 logrotate配置
nginx 日志 logrotate配置如下: /var/log/nginx/*.log { daily missingok rotate 20 compress delaycompress noti ...
- Nginx - 日志格式及输出
1. 前言 在 Nginx 服务器中,如果想对日志输出进行控制还是很容易的.Nginx 服务器提供了一个 HttpLogModule 模块,可以通过它来设置日志的输出格式. 2. HttpLogMod ...
- nginx日志输出配置json格式
修改nginx配置文件 http { include mime.types; default_type application/octet-stream; charset utf-8; # 原有日志格 ...
- Nginx简介及配置介绍
一.什么是Nginx Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发 ...
随机推荐
- 给大家一些改善 Python 程序的 91 个建议
读了一本还不错的书「编写高质量代码改善 Python 程序的 91 个建议」,大多数的建议是真心不错,我虽然写python也有3年多了,但是有些地方确实没去注意过,特地整理了一下,给大家参考. 我已经 ...
- State状态模式
1.简介 在日常开发中,某些对象的状态如果发生改变,对应的行为也将发生改变,那么如何在运行时根据对象的状态动态的改变对象的行为,同时不产生紧耦合关系(即使用if else或者swith所带来的紧耦合关 ...
- Go语言下的线程模型
阅读Go并发编程对go语言线程模型的笔记,解释的非常到,好记性不如烂笔头,忘记的时候回来翻一番,在此做下笔记. Go语言的线程实现模型,又3个必知的核心元素,他们支撑起了这个线程实现模型的主要框架: ...
- selenium之 玩转鼠标键盘操作(ActionChains)
用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件——ActionChains sele ...
- spring3.0框架检测方法运行时间测试(转)
主要利用了Spring AOP 技术,对想要统计的方法进行横切处理,方法执行前开始计时,方法执行后停止计时,得到计时方法就是该方法本次消耗时间. 步骤: 首先编写自己的Interceptor类来实现M ...
- FastDFS 分布式文件存储目录
1.fastdfs安装和配置 https://blog.csdn.net/hy245120020/article/details/78658081 2.使用nginx代理fastdfs以及图片裁剪(f ...
- for循环中变量的作用域问题
看下面这一行代码 for (let i = 0; i < 3; i++) { let i = 'abc' console.log(i) } // abc // abc // abc 从上面可以看 ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest(部分题解)
摘要: 本文是The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛现场赛)的部分解题报告,给出了出题率较高的几道题的题解,希望熟悉区域赛的题型,进而对其 ...
- man rpcbind(rpcbind中文手册)
本人译作集合:http://www.cnblogs.com/f-ck-need-u/p/7048359.html RPCBIND() BSD System Manager's Manual RPCBI ...
- MySQL行转列
IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则 ...