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. 如何写一个适配iPhoneX的底部导航

    引言 iPhoneX发布至今已经有将近一年的时间了,各类app都多多少少做了对iPhoneX的适配,那对于我们H5页面该做哪方面的适配呢? 首先了解安全区域(safe area)的概念,它保证了内容在 ...

  2. Swift中空合运算符、闭区间运算符、单侧区间、半开区间

    空合运算符(Nil Coalescing Operator) 用于取代3目判空运算,提供超短的写法比如常规判空写法如下,反正我写java就是这么干的 var anOptionalInt: Int? = ...

  3. 用python自制微信机器人,定时发送天气预报

    0 引言 前段时间找到了一个免费的天气预报API,费了好段时间把这个API解析并组装成自己想用的格式了,就想着如何实现每天发送天气信息给自己.最近无意中发现了wxpy库,用它来做再合适不过了.以下是w ...

  4. tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)

    池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1.tf.layers.max_pooling2d max_pooling2d( in ...

  5. 【个人杂谈】MacBook Pro的使用心得

    上个月刚买的MacBook Pro,苹果就发新版了.... 从apple香港官网入手了一台MacBook Pro,带到公司,用了差不多一个月吧,这里讲讲我对MacBook的看法吧. 先声明一下两点: ...

  6. 内核第三讲,进入ring0,以及编写第一个内核驱动程序.

    内核第三讲,进入ring0,以及编写第一个内核驱动程序. PS: 请下配置双机调试,下方有可能用到.如果不配置,则你可以不用调试, 博客连接: http://www.cnblogs.com/iBina ...

  7. Python从入门到精通系列文章总目录

    Python最新全套课程(8月中旬开的课),共四个月.所有课件,项目源码,课后习题和答案都包括在内. 包括:Python实战项目引入.Python基础.爬虫基础.爬虫库.Scrapy爬虫框架.动态页面 ...

  8. springboot情操陶冶-SpringApplication(二)

    承接前文springboot情操陶冶-SpringApplication(一),本文将对run()方法作下详细的解析 SpringApplication#run() main函数经常调用的run()方 ...

  9. python使用多线程

    threading 模块支持守护线程, 其工作方式是:守护线程一般是一个等待客户端请求服务的服务器. 如果把一个线程设置为守护线程,进程退出时不需要等待这个线程执行完成. 如果主线程准备退出时,不需要 ...

  10. Git命令速查

    Alias 下面的只是例子,想改成什么跟随自己的意愿即可. git config --global alias.st status //status 缩写成 st git config --globa ...