nginx日志配置

admin 2022年4月20日20:17:29评论33 views字数 2328阅读7分45秒阅读模式

Nginx 日志配置


0x00 前言

日志使用用来进行数据统计、问题排错的重要手段。本文主要介绍 nginx 日志相关的配置如 access_log、log_format、log_not_found、rewrite_log、error_log 以及 Nginx 日志切割。

0x01 日志相关配置

nginx日志相关涉及的配置有:access_log:访问日志;log_format:日志格式;rewrite_log:重定向日志;error_log:错误日志;

nginx 具备非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过 log_format 命令来定义。

· access_log 配置 
语法:
·access_log path [format [buffer=size [flush=time]]];
·access_log path format gzip[=level] [buffer=size] [flush=time];
·access_log syslog:server=address[,parameter=value] [format];
·access_log off; #不记录日志

默认值: access_log logs/access.log combined
使用默认 combined 格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined; 
配置段: http, server, location, if in location, limit_except 

参数解释:
gzip:压缩等级。
buffer:设置内存缓存区大小。
flush:保存在缓存区中的最长时间。

· log_format配置
语法:log_format name string ……; 
默认值: log_format combined "……"; 
配置段: http
释义:name 表示格式名称,string 表示等义的格式。log_format 有一个默认的无需设置的 combined 日志格式,相当于 apache 的 combined 日志格式。 

示例1:
 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                   '$status $body_bytes_sent "$http_referer" '                   '"$http_user_agent"';
示例2:
 log_format  proxy  '$remote_addr - $remote_user [$time_local] "$request" '                   '$status $body_bytes_sent "$http_referer" '                   '"$http_user_agent" "$http_user_agent" ';
· 配置相关变量释义:

$remote_addr:表示客户端地址;
$remote_user:表示http客户端请求Nginx认证的用户名;
$time_local:Nginx通用日志格式下的本地时间;
$request:request请求行,请求的URL、GET等方法、HTTP协议版本;
$request_length:请求的长度;
$request_time:请求处理时间,单位为秒,精度为毫秒;
$status:response返回状态码;
$body_bytes_sent:发送给客户端的字节数,不包括响应头的大小,即服务端响应给客户端body信息大小;
$http_referer:http上一级页面,即从哪个页面链接访问过来的,用于防盗链、用户行为分析;
$http_user_agent:http头部信息,记录客户端浏览器相关信息;
$connection:连接的序列号;
$connection_requesta:当前通常一个连接获得的请求数量;
$msec:日志写入时间,单位为秒,精度为毫秒;
$pipe:如果请求是通过HTTP流水线(pipelined)发送,pipe值为‘p’,否则为“.”;
$http_x_forwarded_for:http请求携带的http信息。

提示:如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。$remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。

· rewrite_log配置 
语法: rewrite_log on | off;
默认值:rewrite_log off; 
配置段:http,server,location,if
作用:由ngx_http_rewrite_module模块提供的。用来记录重写日志的,对于调试重写规则建议开启。启用时将在error log中记录notice级别的重写日志。

· error_log配置 
语法:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; 
默认值:error_log logs/error.log error; 
配置段:main,http,server,location
作用:配置错误日志。
 
*本文章仅供技术交流分享,请勿做未授权违法攻击,雨笋教育不负任何责任。具体请参考《网络安全法》。

nginx日志配置

学习资料

扫码咨询

原文始发于微信公众号(雨笋君):nginx日志配置

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年4月20日20:17:29
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   nginx日志配置http://cn-sec.com/archives/930261.html

发表评论

匿名网友 填写信息