Nginx日志格式配置

系列文章目录



前言

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男女通用,看懂了就去分享给你的码吧。
在这里插入图片描述


Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在这里插入图片描述
Centos上安装Nginx

http://www.javacui.com/service/493.html

error.log 主要是处理http请求错误和nginx本身服务错误状态,按照不同的错误级别记录

access_log 主要是记录处理每次http请求访问的状态

# 关闭当前层级上的指定日志,即不记录日志
# access_log off;
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;

注意log_format必须定义在http下,access_log可以在各自location下定义。

有时候我们可以把日志配置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"
}';

注意:路径path必须存在,如果开启了gzip日志压缩,则不能通过控制台实时查看日志了。

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,否则显示为 .

$request_body

打印请求的参数,如果是GET请求会在$request直接显示,但是Post需要配置这个参数

  • 30
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java毕设王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值