Nginx日志配置详解

本文介绍了Nginx的日志格式,包括基本格式和自定义的JSON格式,以及stream模块的配置。日志变量如$remote_addr、$status和$time_local等被详细解释,并展示了如何优化日志以包含更多信息,如请求时间、响应状态码和客户端信息。
摘要由CSDN通过智能技术生成

日志基本格式

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 logs/access.log main;

main:仅为该日志格式的一个名称,可以随便起,但需要注意,使用某个格式,一定要在access_log后面加上这个名字,否则无效

日志配置变量

https://www.cnblogs.com/wangyingshuo/p/16386047.html

变量含义
$bytes_sent发送给客户端的总字节数
$body_bytes_sent发送给客户端的字节数,不包括响应头的大小
$connection连接序列号
$connection_requests当前通过连接发出的请求数量
$msec日志写入时间,单位为秒,精度是毫秒
$pipe如果请求是通过http流水线发送,则其值为"p",否则为“."
$request_length请求长度(包括请求行,请求头和请求体)
$request_time请求处理时长,单位为秒,精度为毫秒,从读入客户端的第一个字节开始,直到把最后一个字符发送张客户端进行日志写入为止
$status响应状态码
$time_iso8601标准格式的本地时间,形如“2017-05-24T18:31:27+08:00”
$time_local通用日志格式下的本地时间,如"24/May/2017:18:31:27 +0800"
$http_referer请求的referer地址。
$http_user_agent客户端浏览器信息。
$remote_addr客户端IP
$http_x_forwarded_for当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置。
$request完整的原始请求行,如 “GET / HTTP/1.1”
$remote_user客户端用户名称,针对启用了用户认证的请求
$request_uri完整的请求地址,如 “https://daojia.com/”

优化日志配置

JSON格式日志

 log_format json '{"@timestamp":"$time_iso8601",'
                  '"localhost":"$server_addr",'
                  '"service":"nginxServer",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"body_size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"url":"$uri",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';

普通格式

log_format  main  '$remote_addr - [$time_local] - "$request" - "$request_time" '
                  '- $status - $body_bytes_sent;

stream模块配置格式

log_format basic '"clientHost":$remote_addr - [$time_local] - '
				 '"protocol":$protocol - "status":$status - "sentBytes":$bytes_sent - "receivedBytes":$bytes_received - '
                 '"requestTime":$session_time';
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值