Java小强个人技术博客站点    手机版
当前位置: 首页 >> 软件 >> Nginx日志格式配置

Nginx日志格式配置

36651 软件 | 2022-2-3

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需要配置这个参数


END

推荐您阅读更多有关于“ 配置 日志 nginx log_format access_log ”的文章

上一篇:Nginx配置文件conf解释 下一篇:Nginx启动关闭重启用脚本实现

猜你喜欢

发表评论:

评论:

回复 Java小强 评论于 2022-02-03 21:48
nginx 全局变量  

$args : #这个变量等于请求行中的参数,同$query_string
$content_length : 请求头中的Content-length字段。
$content_type : 请求头中的Content-Type字段。
$document_root : 当前请求在root指令中指定的值。
$host : 请求主机头字段,否则为服务器名称。
$http_user_agent : 客户端agent信息
$http_cookie : 客户端cookie信息
$limit_rate : 这个变量可以限制连接速率。
$request_method : 客户端请求的动作,通常为GET或POST。
$remote_addr : 客户端的IP地址。
$remote_port : 客户端的端口。
$remote_user : 已经经过Auth Basic Module验证的用户名。
$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme : HTTP方法(如http,https)。
$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
$server_name : 服务器名称。
$server_port : 请求到达服务器的端口号。
$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri : 与$uri相同。