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 日志格式配置介绍的更多相关文章

  1. nginx日志格式配置

    我一向对日志这个东西有些许恐惧,因为在分析日志是需要记住不同服务器日志的格式,就拿提取ip这一项来说,有的服务器日志是在第一列,有的是第二列或则第三列等等.知道今天我才发现,日志格式是可以自定义配置的 ...

  2. 运维技巧-Nginx日志格式

    1.说一说 当你安装完nginx,输出的格式是比较乱的,这样我们就需要自己去定义一下,自己看着舒服的格式. 2.Nginx日志字段 $remote_addr 记录客户端IP,但她的值不是客户端提供的, ...

  3. Nginx日志格式以及相关配置

    一.Nginx日志格式以及参数说明log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' ...

  4. Nginx日志文件配置与切割

    Nginx日志的指令主要有两条: log_format,设置日志的格式 access_log,指定日志文件的存放路径.格式和缓存大小 两条指令在Nginx配置文件中的位置可以在http{……..}之间 ...

  5. ELK 之三:Kibana 使用与Tomcat、Nginx 日志格式处理

    一:kibana安装: kibana主要是搜索elasticsearch的数据,并进行数据可视化的展现,新版使用nodejs. 1.下载地址: https://www.elastic.co/downl ...

  6. nginx日志 logrotate配置

    nginx 日志 logrotate配置如下: /var/log/nginx/*.log { daily missingok rotate 20 compress delaycompress noti ...

  7. Nginx - 日志格式及输出

    1. 前言 在 Nginx 服务器中,如果想对日志输出进行控制还是很容易的.Nginx 服务器提供了一个 HttpLogModule 模块,可以通过它来设置日志的输出格式. 2. HttpLogMod ...

  8. nginx日志输出配置json格式

    修改nginx配置文件 http { include mime.types; default_type application/octet-stream; charset utf-8; # 原有日志格 ...

  9. Nginx简介及配置介绍

    一.什么是Nginx Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发 ...

随机推荐

  1. Asp.net 性能监控之压测接口“卡住” 分析

    问题描述:web api项目接口压测.前期并发100,500没出现问题,平均耗时也在几百毫秒.当并发1000时候,停留等待许久,看现象是jemeter卡住,没返回,时间过了许久,才正常. 解决过程: ...

  2. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

  3. 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList的问题

    一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...

  4. 03 使用Tensorflow做计算题

    我们使用Tensorflow,计算((a+b)*c)^2/a,然后求平方根.看代码: import tensorflow as tf # 输入储存容器 a = tf.placeholder(tf.fl ...

  5. MaskRCNN-Keypoints

    这个月先写一篇吧,后面要复习数学考试了,可能到时候就忘了.今天写一个比较有意思的东西,关于人体的分割与姿态估计.如下图所示: 图片选自mask rcnn的论文,这里由于时间的关系,就不多叙述技术细节了 ...

  6. Spring-WebSocket 教程

    WebSocket 教程 概述 WebSocket 是什么? WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 T ...

  7. 一篇文章详解python的字符编码问题

    一:什么是编码 将明文转换为计算机可以识别的编码文本称为“编码”.反之从计算机可识别的编码文本转回为明文为“解码”. 那么什么是明文呢,首先我们从一段信息说起,消息以人们可以理解,易懂的表示存在,我们 ...

  8. Jenkins结合.net平台工具之ReportGenerator

    上一节我们讲解了如何使用opencover生成单元测试覆盖率报告,opencover默认生成的report为xml格式,可读性并不是特别强,如果靠阅读opencover生成的results.xml来分 ...

  9. Perl匿名数组、hash和autovivification特性

    可有构建匿名的对象,这样就没必要去为只用一两次的数组.hash去取名字,有时候取名是很烦的事. 使用中括号[]构建匿名数组 使用大括号{}构建匿名hash 不包含任何元素的[]和{}分别是匿名空数组. ...

  10. Spring DAO模块

    Spring的DAO模块提供了对JDBC.Hibernate.Mybatis等DAO层支持,本节介绍DAO模块对JDBC的支持.DAO模块依赖ommons-dbcp.jar.commons-pool. ...